两个表上的mysql内连接

时间:2013-12-17 21:17:29

标签: mysql join inner-join

wp_users中的列:

id
user_login

wp_usermeta中的列:

 user_id
 meta_key [if equals 'primaryblog']
 metav_value

wp_users中的ID和wp_usermeta中的用户ID相同。我期待结果为

id, user_login, meta_key, meta_value

我试过了:

select a.user_id,a.meta_key,a.meta_value 
from wp_usermeta as a 
where meta_key = 'primaryblog' 
inner join b.id, b.user_login 
from wp_users as b on a.user_id=b.id

如何获得预期的结果?

1 个答案:

答案 0 :(得分:1)

JOIN在WHERE子句之前:

SELECT 
  a.id, a.user_login, b.meta_key, b.meta_value
FROM wp_users a
JOIN wp_usermeta b ON a.id = b.user_id
WHERE meta_key = 'primaryblog';