Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.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
Php SELECT with LEFT JOIN返回空值_Php_Mysql_Join_Left Join - Fatal编程技术网

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在这种情况下,请删除帖子。