我有 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
值(value)。任何想法如何正确地将两个查询合并为一个?感谢阅读。
最佳答案
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';
关于mysql - 从两个表mysql中选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27337259/