Mysql 如何在可能不发生的情况下连接两个表?
我有两张桌子Mysql 如何在可能不发生的情况下连接两个表?,mysql,sql,Mysql,Sql,我有两张桌子 表用户,具有ID字段 表选项,包含userId、optionName和value 如何选择选项名称为'email'且值为1或选项表中没有电子邮件条目的用户 试试这个: select * from User u inner join Option o on u.id = o.userid where (o.optionName = 'email' and o.value = 1) or (o.optionName <> 'email') u.id=o.userid上
- 表用户,具有ID字段
- 表选项,包含userId、optionName和value
select * from User u inner join Option o on u.id = o.userid
where (o.optionName = 'email' and o.value = 1) or (o.optionName <> 'email')
u.id=o.userid上从用户u内部连接选项o中选择*
其中(o.optionName='email'和o.value=1)或(o.optionName'email')
这可能会帮助您:
"SELECT * from users u left join options o on u.id = o.userid where (o.optionName = 'email' and o.value = 1) or o.userid is null; ";
为什么
o.value=0
?如果有value=2,那么它将失败,因为OP明确提到他想要value=1
我想我复制了您的查询并进行了编辑。我现在就纠正它。