gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:52:05 34 4
gpt4 key购买 nike

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

它不返回painting.id_grouppainting.id_typepainting.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

更新:它显示 显示第 0 - 0 行(总共 1 行,查询耗时 0.0004 秒)。删除后

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

关于mysql - SQL - 如何从同一列中选择多个表并连接多行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8087444/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com