Php SELECT with LEFT JOIN返回空值
我有两张桌子: 帖子:Php SELECT with LEFT JOIN返回空值,php,mysql,join,left-join,Php,Mysql,Join,Left Join,我有两张桌子: 帖子: id | id_author | content | year 1 | 1 | hello | 2015 2 | 1 | world | 2016 3 | 2 | hi | 2016 id | id_author | id_post | year 1 | 3 | 2 | 2016 收藏夹: id | id_author | content | year 1 | 1
id | id_author | content | year
1 | 1 | hello | 2015
2 | 1 | world | 2016
3 | 2 | hi | 2016
id | id_author | id_post | year
1 | 3 | 2 | 2016
收藏夹:
id | id_author | content | year
1 | 1 | hello | 2015
2 | 1 | world | 2016
3 | 2 | hi | 2016
id | id_author | id_post | year
1 | 3 | 2 | 2016
我想做一个选择
,通过收藏夹
使用左连接
获取帖子
数据。所以,我做到了:(
考虑到我是用id_author=3的用户登录的)
所以,我做了:($sql
等于上面的查询)
但结果是:
array(1) {
[0]=>
array(5) {
["id"]=>
NULL
["id_author"]=>
NULL
["content"]=>
NULL
["year"]=>
NULL
["id_post"]=>
string(1) "2"
}
}
为什么返回
NULL
?鉴于OP的架构和数据,以下查询在语法上无效:
SELECT p.id, p.id_author, p.content, p.year, f.id_post
FROM favorites f
LEFT JOIN posts p
ON f.id_post = p.id
WHERE id_author = 3
ORDER BY f.id DESC
由于WHERE
子句的谓词使用的字段存在歧义:id\u author=3
您很可能有p.id\u author=3
,这意味着LEFT JOIN
操作的右表字段由WHERE
子句使用。这将左连接
转换为内部连接
操作,不产生任何结果。因此,您可能必须使用:
WHERE f.id_author = 3
给定OP的架构和数据,以下查询在语法上无效:
SELECT p.id, p.id_author, p.content, p.year, f.id_post
FROM favorites f
LEFT JOIN posts p
ON f.id_post = p.id
WHERE id_author = 3
ORDER BY f.id DESC
由于WHERE
子句的谓词使用的字段存在歧义:id\u author=3
您很可能有p.id\u author=3
,这意味着LEFT JOIN
操作的右表字段由WHERE
子句使用。这将左连接
转换为内部连接
操作,不产生任何结果。因此,您可能必须使用:
WHERE f.id_author = 3
对不起,我编辑了我的问题,事实上没有歧义,我只是写错了条件。。。该查询现在是正确的。@Igor在这种情况下请删除帖子。对不起,我编辑了我的问题,事实上没有歧义,我只是写了错误的条件。。。查询正在进行中。@Igor在这种情况下,请删除帖子。