Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.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 复杂SQL查询返回结果(如果不匹配)_Mysql_Sql - Fatal编程技术网

Mysql 复杂SQL查询返回结果(如果不匹配)

Mysql 复杂SQL查询返回结果(如果不匹配),mysql,sql,Mysql,Sql,我编写了一个复杂的SQL查询(可以正确运行): select user.id,user.name,profile.info,score.ammount from user,profile,score where email = 'example@email.com' AND user.id = profile.user_id AND user.id = score.user_id AND profile.type = 'language' AND score.type = 'amount' 此

我编写了一个复杂的SQL查询(可以正确运行):

select user.id,user.name,profile.info,score.ammount
from user,profile,score
where email = 'example@email.com'
AND user.id = profile.user_id
AND user.id = score.user_id
AND profile.type = 'language'
AND score.type = 'amount'
此查询将返回:

[id] => 10002096
[name] => Erik
[info] => English
[ammount] => 510710
问题是,如果user.id与profile.user_id不匹配,那么之前匹配的结果(user.id,user.name)不会返回?(即使未找到下一个匹配项,我也要将其返回)

如果不匹配,则希望返回如下内容:

[id] => 10002096
[name] => Erik
如何解决这个问题

配置文件表示例:

id          user_id    type         info  
1             1        language     english
2             1        admin        top  
3             2        likes        football
4             3        likes        -
5             3        language     english

id=2的用户没有语言

请尝试左键加入表,而不是获取笛卡尔乘积并进行筛选。。这是ansi连接表的老方法,当添加更多表时,处理起来更困难

SELECT u.id, u.name, p.info, s.ammount
FROM user u
LEFT JOIN profile p ON p.user_id = u.id AND p.type = 'language'
LEFT JOIN score s ON s.user_id = u.id AND s.type = 'amount'
WHERE email = 'example@email.com'

基本上,左边的join表示加入信息,如果有不匹配的行

则不进行筛选,因为看不到任何数据,我的第一反应是“嗯?”但也许tyr会将一些and改为OR?你应该向我们展示一些数据,让我们理解你的意思。你对什么和什么意思有一个基本的误解。为了返回一个结果,它必须传递所有的AND语句。放弃老式的逗号语法进行连接操作。改为使用JOIN关键字,并将JOIN谓词从WHERE子句移动到ON子句。这将使您能够使用
左连接执行“外部”连接操作,仅供参考OP的查询不是笛卡尔结果,它们只是在where子句中使用带有连接条件的“旧”逗号连接语法。@bluefeet我知道。。很抱歉没有完全说明这一点。这是ansi的老办法。。但通常不是一个好方法。它也有同样的问题(如果p.type='language'未找到)没有返回结果?@mwafi那么您能提供一些数据吗?给我几行,其中p.type不是语言well@JohnRuddell无法保证在配置文件表中找到(类型=语言)(将更新qustion)