Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在可能不发生的情况下连接两个表?_Mysql_Sql - Fatal编程技术网

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
如何选择选项名称为'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上从用户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
我想我复制了您的查询并进行了编辑。我现在就纠正它。