簡體   English   中英

從兩個表中選擇值mysql

[英]selecting values from two tables mysql

我有2個表accountsconfirm_email confirm_email有一個名為列codeaccounts有一個叫列email ,他們都有一列稱為account_name我想選擇的codeconfirm_emailemailaccounts ,其中的價值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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM