我有一个MYSQL查询,它正常工作,除了我想对两个语句执行OUTER连接,以便它们显示第一个表的行,而不需要在第二个表中有相应的行。查询如下
SELECT wp_usermeta.user_id,
m9.id as group_id,
wp_usermeta.meta_value as nickname,
m2.meta_value as abbreviation,
m3.meta_value as name,
m4.meta_value as games,
m5.description as description,
m6.slug as slug,
m7.date_created as date_created,
m8.meta_value as total_member_count,
m10.status as status,
m11.membership_id as type
FROM wp_usermeta
INNER JOIN wp_usermeta as m2 ON wp_usermeta.user_id = m2.user_id
INNER JOIN wp_usermeta as m3 ON wp_usermeta.user_id = m3.user_id
INNER JOIN wp_usermeta as m4 ON wp_usermeta.user_id = m4.user_id
INNER JOIN wp_bp_groups as m5 ON wp_usermeta.user_id = m5.creator_id
INNER JOIN wp_bp_groups as m6 ON wp_usermeta.user_id = m6.creator_id
INNER JOIN wp_bp_groups as m7 ON wp_usermeta.user_id = m7.creator_id
INNER JOIN wp_bp_groups as m9 ON wp_usermeta.user_id = m9.creator_id
INNER JOIN wp_bp_groups_groupmeta as m8 ON m9.id = m8.group_id
# INNER JOIN wp_2_pmpro_memberships_users as m10 ON wp_usermeta.user_id = m10.user_id
# INNER JOIN wp_2_pmpro_memberships_users as m11 on wp_usermeta.user_id = m11.user_id
WHERE (wp_usermeta.meta_key = 'nickname' AND
m2.meta_key = 'abbreviation' AND
m3.meta_key = 'name' AND
m4.meta_key = 'games' AND
m8.meta_key = 'total_member_count' AND
m10.status = 'active' AND
m11.status = 'active'
)
根据this SO帖子我尝试使用格式
SELECT * FROM t1
LEFT JOIN t2 ON t1.id = t2.id
UNION
SELECT * FROM t1
RIGHT JOIN t2 ON t1.id = t2.id
将标有#above的两行更改为
INNER JOIN (
SELECT * FROM wp_usermeta
LEFT JOIN wp_2_pmpro_memberships_users ON wp_usermeta.user_id = wp_2_pmpro_memberships_users.user_id
UNION
SELECT * FROM wp_usermeta
RIGHT JOIN wp_2_pmpro_memberships_users ON wp_usermeta.user_id = wp_2_pmpro_memberships_users.user_id
)
但它似乎不起作用。 MySQL抛出错误
我的SQL查询知识是基本的,仅限于简单的数据库查询。任何帮助将不胜感激。
答案 0 :(得分:1)
在一个表中找到您在另一个表中没有的ID的基本方法是
Select * from t1 where t1.commonid not in(select commonid from t2);
这种方法很有效,但随着桌子的增长而变化非常大。所以更好的方法是
Select t1.*
FROM t1 left join t2 on t1.commonid = t2.commonid
where t2.id is null
左连接为您提供来自t1的所有行以及来自t2的相应行,来自t1的行(t2中没有对应的行)对于所有t2值返回null。所以只需使用任何t2字段" not null"集。
答案 1 :(得分:1)
在我看来,你只是在这之后:
SELECT wp_usermeta.user_id,
m9.id as group_id,
wp_usermeta.meta_value as nickname,
m2.meta_value as abbreviation,
m3.meta_value as name,
m4.meta_value as games,
m5.description as description,
m6.slug as slug,
m7.date_created as date_created,
m8.meta_value as total_member_count,
m10.status as status,
m11.membership_id as type
FROM wp_usermeta
INNER JOIN wp_usermeta as m2 ON wp_usermeta.user_id = m2.user_id
INNER JOIN wp_usermeta as m3 ON wp_usermeta.user_id = m3.user_id
INNER JOIN wp_usermeta as m4 ON wp_usermeta.user_id = m4.user_id
INNER JOIN wp_bp_groups as m5 ON wp_usermeta.user_id = m5.creator_id
INNER JOIN wp_bp_groups as m6 ON wp_usermeta.user_id = m6.creator_id
INNER JOIN wp_bp_groups as m7 ON wp_usermeta.user_id = m7.creator_id
INNER JOIN wp_bp_groups as m9 ON wp_usermeta.user_id = m9.creator_id
INNER JOIN wp_bp_groups_groupmeta as m8 ON m9.id = m8.group_id
# LEFT JOIN wp_2_pmpro_memberships_users as m10
ON wp_usermeta.user_id = m10.user_id
AND m10.status = 'active'
# LEFT JOIN wp_2_pmpro_memberships_users as m11
ON wp_usermeta.user_id = m11.user_id
AND m11.status = 'active'
WHERE (wp_usermeta.meta_key = 'nickname' AND
m2.meta_key = 'abbreviation' AND
m3.meta_key = 'name' AND
m4.meta_key = 'games' AND
m8.meta_key = 'total_member_count'
)