针对连接表的MySQL多个别名连接

时间:2017-02-20 14:37:54

标签: mysql

我有这个查询,工作正常:

select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'

所以,它在表1上查询了表2中的连接,然后是表2中表3的多个别名连接的后续连接,但是一旦我添加了其他连接,我就没有得到任何结果而且我和#39;我不确定为什么,例如:

select table_1.*, coalesce(test_1.type, test_2.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
inner join `table_3` as `test_2` on `test_2`.`code` = `table_2`.`column` and `table_2`.`column` = 'H'

有谁能解释我做错了什么?

2 个答案:

答案 0 :(得分:1)

尝试--num_top_predictions加入LEFT。如果table_3没有记录,那么由于table_3加入,这就是您没有产生任何结果的原因。

实际上,您没有加入INNER上的任何列。最有可能是问题。

答案 1 :(得分:1)

第二个查询的预期样本结果是什么?
你可以试试这个问题吗?

select table_1.*, coalesce(test_1.type) as type 
from `tbl_1` 
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id` 
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and         
   (`table_2`.`column` = 'L' or `table_2`.`column` = 'H')