[英]combine mysql tables with query UNION or JOIN?
全新的復雜mysql查詢。 我有一個數據庫架構,其中包含一對需要組合的表:
在第一個表格avatars,
我有user_id
, url
和name,
列,每個唯一的user_id.
都有一行user_id.
在第二個表interpretations
我有user_id
和scan_index
以及其他3個字段。 我想生成一個與interpretations,
中的特定scan_index
匹配的mysql結果interpretations,
包括該表中的所有列以及其他對應字段url
和avatars.
name
avatars.
我應該使用JOIN還是UNION進行此查詢?
JOIN用於在列值的基礎上合並兩個表(在這里,我想是它的user_id),您的查詢可以是這樣的:
SELECT url, name, scan_index,...
FROM avatars
LEFT JOIN interpretations
ON avatars.user_id = interpretations.user_id
WHERE interpretations.scan_index = *your_specific_value*
您需要使用join
。 非常簡單地解釋它,一個join
通過將彼此相鄰從2個表中的數據,而聯合收割機2台union
通過將低於對方的數據集2次的查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.