小编典典

SQL-如何从同一列中选择多个表并联接多个行?

sql

如何选择多个表并从同一列联接多个行?

它不会返回painting.id_grouppainting.id_type并且painting.id_location。我确定此SQL代码段的语法错误。

SELECT painting.id, painting.order, painting_en.url, id_portfolio.en
FROM painting
LEFT JOIN painting_en
USING ( id ) 
LEFT JOIN id_portfolio ON id_portfolio.id = painting.id_group
AND id_portfolio.id = painting.id_type
AND id_portfolio.id = painting.id_location
WHERE painting_en.url='2011-name3'
LIMIT 1

更新: 显示Showing rows 0 - 0 ( 1 total, Query took 0.0004 sec)。移除后

AND id_portfolio.id = painting.id_type
AND id_portfolio.id = painting.id_location

它返回:

id        | order | url        |en
----------+-------+------------+-----
09518709  | 4     | 2011-name3 | NULL

但我需要退货:

id        | order | url        | group   | type          | location
----------+-------+------------+---------+---------------+---------
09518709  | 4     | 2011-name3 | realism | oil on canvas | US

阅读 323

收藏
2021-04-14

共1个答案

小编典典

您可以多次连接到同一张表,而只需提供不同的别名

SELECT painting.id, 
       painting.order, 
       painting_en.url, 
       `group`.en AS `group`, 
       TYPE.en AS type, 
       location.en AS location
FROM   painting 
       LEFT JOIN painting_en 
       USING (id) 
       LEFT JOIN id_portfolio `group` 
         ON `group`.id = painting.id_group 
       LEFT JOIN id_portfolio TYPE 
         ON TYPE.id = painting.id_type 
       LEFT JOIN id_portfolio location 
         ON location.id = painting.id_location 
WHERE  painting_en.url = '2011-name3' 
LIMIT  1
2021-04-14