0
I’m trying to make a SELECT
in a table conta
, in this table I have a foreign key
to the table venda
, in certain cases this foreign key
is null and I want to return the information of the sale when this key is not null and ignore when it is, I am trying to do this through a left join
, but I’m not getting it.
How to do this ?
I’m trying like this.
select * from conta t1
inner join cliente t2 on (t2.id = t1.cliente_id)
left join (
select * from venda t3
inner join itemvenda t4 on (t4.venda_id = t3.id)
inner join produto t5 on (t4.produto_id = t5.id)
) ON ((t1.venda_id is not null) AND t1.venda_id = t3.id)
where (t1.status = 0) AND (t1.tipoConta = 1) AND (t1.dtVencimento BETWEEN "2016/10/01" AND "2016/10/28")
ORDER BY t2.nome, t1.dtVencimento
Exception
select * from conta t1
inner join cliente t2 on (t2.id = t1.cliente_id)
left join (
select * from venda t3
inner join itemvenda t4 on (t4.venda_id = t3.id)
inner join produto t5 on (t4.produto_id = t5.id)
) ON ((t1.venda_id IS NOT NULL) AND t1.venda_id = t3.id)
where (t1.status = 0) AND (t1.tipoConta = 1) AND (t1.dtVencimento BETWEEN "2016/10/01" AND "2016/10/28")
ORDER BY t2.nome, t1.dtVencimento LIMIT 0, 25
Mensagens do MySQL : Documentação
#1248 - Every derived table must have its own alias
lacked a
as
on the part ofleft Join () as c
and continues... has an error oft3
is not the correct alias would be:) as c ON ((t1.venda_id IS NOT NULL) AND t1.venda_id = c.id)
– novic
@Bacco apparently this duplicate does not appear to be the best situation I need.
– FernandoPaiva
Note that there is only effective closure if other people have the same impression and vote the same way, but even so you can always [Dit] the question to add details that differentiate more, or even leave a comment for us to know what was the problem with the
IS NULL
. Closure is an organization tool, not a penalty tool. The idea is that one way or another you get out of here with the problem solved. But for this, you need to detail the exact difficulty, so that we know how to help. The first step would be to fix the missing Alias (AS nome
).– Bacco
No doubt the error is in the alias. .. You are trying to access the T3 that is within another context.
– Marco Souza
I don’t know what was your logic for making a left Join with a sub select being that you didn’t make any filter or selected only some fields of the tables, your select could have been foito with left Join straight into the tables without the need for su subselect...
– Marco Souza