Problem with INNER JOIN

Asked

Viewed 92 times

3

Good night,

I’m making a inner join 3 tables but I’m not getting it to work must have some mistake that I’m not able to detect or do the right way I’m new to this 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
  • Select *. (dot not comma), see if you can solve.

  • Does not resolve with the dot when testing directly in phpmyadmin of the error of SQL syntax with the comma of Operand should contain 1 column(s)

2 answers

3


I think the mistake is here:

(SELECT * FROM categorias_estabelecimentos WHERE categoria_slug = :categoria_slug) 

Only one field must return

Soon it must be so:

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

But try to do it the way below because it is more perfomatic:

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

1

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

In this case your return from sub-SELECT must return a single column, or else you must use a function so that the data becomes a single record. Remembering further that you should name this return through the 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.