如何选择多个表并从同一列联接多个行?
它不会返回painting.id_group,painting.id_type并且painting.id_location。我确定此SQL代码段的语法错误。
painting.id_group
painting.id_type
painting.id_location
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)。移除后
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
您可以多次连接到同一张表,而只需提供不同的别名
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