如果列值不存在,则不要加入表 [英] Don't join a table if column value doesn't exist

查看:38
本文介绍了如果列值不存在,则不要加入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SELECT
`wp_emu_theme_contact_page`.`firstName`
, `wp_emu_theme_contact_page`.`city`
, `wp_emu_theme_contact_page`.`state`
, `wp_posts`.`post_content`
, `wp_posts`.`post_date`
, `wp_posts`.`post_type`
, `wp_posts`.`ID`
, `wp_emu_theme_testimonials`.`permission`
, `wp_emu_theme_testimonials`.`userID`
, `wp_users`.`user_login`
, `wp_usermeta`.`meta_value`
FROM
 `wp_emu_theme_testimonials`
INNER JOIN  `wp_emu_theme_contact_page` 
    ON (`wp_emu_theme_testimonials`.`contactID` = `wp_emu_theme_contact_page`.`dbID`)
INNER JOIN  `wp_posts` 
    ON (`wp_emu_theme_testimonials`.`postID` = `wp_posts`.`ID`)
LEFT JOIN  `wp_users` 
    ON (`wp_emu_theme_testimonials`.`userID` = `wp_users`.`ID`)
INNER JOIN  `wp_usermeta` 
    ON (`wp_usermeta`.`user_id` = `wp_users`.`ID`)
WHERE (`wp_posts`.`post_type` ='testimonial'
AND `wp_usermeta`.`meta_key` ='facebook_uid')
OR (`wp_usermeta`.`meta_key` = 'null');

问题是表 wp_usermeta 可能没有 meta_key = 'facebook_uid'.在这种情况下,我没有收到这些条目.我想获得所有其他东西,即使那个 user_id 不存在 meta_key.如果是这样,那就太好了.如果我取出有关该表的部分,此查询将按预期工作.我试过没有手术室.我用的是mysql.

The problem is that table wp_usermeta might not have a meta_key = 'facebook_uid'. In that case, I am not getting those entries. I want to get all the other stuff, even if that meta_key doesnt exist for that user_id. And if it does, great. This query works as expected if I take out the parts regarding that table. I've tried without the OR. I am using mysql.

推荐答案

听起来您想使用 LEFT JOIN 并使 ON 的条件部分成为:

It sounds like you want to use LEFT JOIN and make that conditional part of the ON:

LEFT JOIN  `wp_usermeta` 
    ON (`wp_usermeta`.`user_id` = `wp_users`.`ID`
        AND `wp_usermeta`.`meta_key` ='facebook_uid')

这篇关于如果列值不存在,则不要加入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆