我可以通過此查詢獲得一些幫助。我會詳細解釋。爲了更容易,我將以HTML標籤和屬性爲例。MySQL查詢建議請求
我有3個表:
- TBL 1 - 包含了所有的HTML標籤(
tagId
,tagName
) - tbl2的 - 包含了所有可能出現的 標籤可用屬性(
attId
,attName
) - TBL3 - 是一個映射表TBL1和TBL2(
tagId
,attId
)
我想要選擇屬於所選標籤(在標籤ID爲4的示例中)和標籤ID的所有屬性(以及有關屬性的相關信息)。
這裏是我想從查詢得到什麼一個例子:
attId tagId attName
50 4 The name of the attribute with id 50
89 4 The name of the attribute with id 89
114 4 The name of the attribute with id 114
下面是我做了查詢,但我相信有一個更好的辦法。
SELECT tbl2.*, tbl3.tagId
FROM tbl2 JOIN tbl3
WHERE tbl3.attId IN (
SELECT tbl3.attId FROM tbl3 where tbl3.tagId=4
)
AND tbl3.attIdd = tbl2.attId
GROUP BY tbl3.attId
在此先感謝。
感謝很多驚人的答案。顯然我需要閱讀更多關於JOIN的內容。再次感謝。 – Boris