table event
id idArtist City Img
__________________________________
1 51 Lonon 01.jpg
.. . .. .. .etc
table artist
id Name Img01 Img02 Img03 Img_Txt01 Img_Txt02 Img_Txt03
_________________________________________________________________________
51 Bob 54.jpg 01.jpg NULL text 01 Text 02
我需要在事件表中显示所有Img并显示存储在Artist表中的Text(你看Img == Img02然后我需要Img_Txt02)
我该怎么办? 很多thxxxxx
答案 0 :(得分:4)
SELECT event.Img,artist.Img_Txt02
FROM event LEFT JOIN artist
ON event.idArtist=artist.id
假设我正确理解你的问题。
答案 1 :(得分:0)
根据OP澄清重做
由于您不知道我们将在艺术家表格中链接哪些字段(img01,img02,img03),我建议构建一个改进艺术家表格布局的视图,以便我们可以链接它。这是我将使用的代码:
SELECT id, Name, Img01 AS Img, Img_Txt01 AS Img_Txt
FROM artist
UNION ALL
SELECT id, Name, Img02, Img_Txt02
FROM artist
UNION ALL
SELECT id, Name, Img03, Img_Txt03
FROM artist
这会为您提供一个可以链接的列表。它还允许您相当简单地增长您的艺术家表(添加Img4,Img5等)您也可以在最终查询中直接使用以上语句,如下所示:
SELECT event.*, images.Img_Txt
FROM event
INNER JOIN
(SELECT id, Name, Img01 AS Img, Img_Txt01 AS Img_Txt
FROM artist
UNION ALL
SELECT id, Name, Img02, Img_Txt02
FROM artist
UNION ALL
SELECT id, Name, Img03, Img_Txt03
FROM artist) AS images
ON event.idArtist = images.id AND event.Img = images.Img_Txt