Problem with INNER JOIN

3

Good evening,

I'm doing a inner join of 3 tables but I can not get it to work I should have some error that I can not detect or do the right way I'm new to it inner join

PHP

SELECT *, (SELECT * FROM categorias_estabelecimentos WHERE categoria_slug = :categoria_slug) FROM estabelecimentos
INNER JOIN estabelecimentos_anexos ON estabelecimentos_anexos.id_mae = estabelecimentos.id
WHERE estabelecimentos.id = categorias_estabelecimentos.estabelecimento_id
AND estabelecimentos.activo = :activo
AND estabelecimentos_anexos.seccao = :seccao
ORDER BY pos ASC
    
asked by anonymous 30.07.2015 / 21:14

2 answers

3

I think the error is here:

(SELECT * FROM categorias_estabelecimentos WHERE categoria_slug = :categoria_slug) 

Only one field should be returned

It should look like this:

(SELECT ce.campo FROM categorias_estabelecimentos ce WHERE ce.categoria_slug = :categoria_slug) 

But try to do it as it is more profitable:

SELECT 
e.*, 
ea.*,
ce.*
FROM 
estabelecimentos e
INNER JOIN estabelecimentos_anexos ea ON ea.id_mae = e.id
INNER JOIN categorias_estabelecimentos ce ON ce.categoria_slug = e.categoria_slug
WHERE 
AND e.activo = :activo
AND ea.seccao = :seccao
    
30.07.2015 / 21:42
1

You are using SELECT within another SELECT or be sub-SELECT .

In this case your return from sub-SELECT should return a single column, or you must use a function to make the data a single record. Remember also that you should name this return through AS .

Example

SELECT
    A.a,
    (
        SELECT
            B.b   // UNICO DADO
        FROM
            table_b B
        WHERE
            B.a = A.a
    ) AS 'A.b'
FROM
    table_a A
    
30.07.2015 / 22:05