[英]selecting values from two tables mysql
我有2個表accounts
和confirm_email
。 confirm_email
有一個名為列code
和accounts
有一個叫列email
,他們都有一列稱為account_name
我想選擇的code
從confirm_email
和email
的accounts
,其中的價值account_name
在兩個相同的,等於具體值。 因此,基本上我想將這兩個命令結合起來:
SELECT email FROM accounts WHERE account_name = 'value';
SELECT code FROM confirm_email WHERE account_name = 'value';
我嘗試了這個:
SELECT code, email FROM confirm_email, accounts WHERE accounts.account_name = 'value';
但這會返回code
列中的所有代碼,並且email
對應於每個code
值所取代的正確code
。 有什么想法如何將兩個查詢正確地組合成一個? 謝謝閱讀。
SELECT c.code, a.email
FROM confirm_email c join accounts a on a.account_name = c.account_name
WHERE a.account_name = 'value';
當聯接兩個表時,需要設置聯接條件。
舊樣式(沒有JOIN關鍵字):
SELECT c.code, a.email
FROM confirm_email c, accounts a
WHERE a.account_name = c.account_name
and a.account_name = 'value';
試試這個內部聯接
SELECT confirm_email.code, accounts.email
FROM confirm_email inner join accounts
ON confirm_email.account_name = accounts.account_name
WHERE accounts.account_name = 'value';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.