Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.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 MySQL中条件不为null的联接表_Php_Mysql - Fatal编程技术网

Php MySQL中条件不为null的联接表

Php MySQL中条件不为null的联接表,php,mysql,Php,Mysql,我正在使用PHP和MySQL创建一个博客。对于搜索引擎,我需要一种方法来限制MySQL结果,以便只返回带有图像的帖子。在帖子和图片之间存在一对多的关系 这是数据库结构: 帖子: posted(主键), userid(FK), Post\u时间戳 Post_时间戳是使用PHP中的time()函数计算的 图像: ImageID(主键), posted(FK), 图像类型, 图像 这是我到目前为止的查询,但有一个语法错误: SELECT DISTINCT Post.PostID, Images

我正在使用PHP和MySQL创建一个博客。对于搜索引擎,我需要一种方法来限制MySQL结果,以便只返回带有图像的帖子。在帖子和图片之间存在一对多的关系

这是数据库结构:

帖子
posted
(主键),
userid
(FK),
Post\u时间戳

Post_时间戳是使用PHP中的time()函数计算的

图像
ImageID
(主键),
posted
(FK),
图像类型
图像

这是我到目前为止的查询,但有一个语法错误:

SELECT
    DISTINCT Post.PostID, Images.ImageID, Images.PostID, Post.Post_timestamp
WHERE
    Post.PostID = Images.PostID
ORDER BY
    Post.Post_timestamp DESC
LIMIT
    1,5;

我使用的分页限制;我想每页五个结果。如果在图像表中也可以找到所述的
PostID
,则结果表应显示
Post.PostID
(不同)。

您忘记了from子句

SELECT
    DISTINCT Post.PostID, Image.ImageID, Image.PostID, Post.Post_timestamp
from
    Posts Post,Images Image
WHERE
    Post.PostID = Image.PostID
ORDER BY
    Post.Post_timestamp DESC
LIMIT
    1,5;
更新:

SELECT
    Post.PostID, Image.ImageID, Image.PostID, Post.Post_timestamp
from
    Posts Post,Images Image
WHERE
    Post.PostID = Image.PostID
GROUP BY
    Post.PostID   
ORDER BY
    Post.Post_timestamp DESC
LIMIT
    1,5;

使用group by

什么是SQL错误?如果您想要前5个结果,请尝试
限制5
。#1064-您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解正确的语法,以便在第1行“WHERE Post.PostID=Images.PostID ORDER BY Post.Post_timestamp DESC LIMIT 1,5”附近使用,当我使用LIMIT 5时,它会执行相同的错误;但是返回的行不明显
PostID ImageID PostID Post_时间戳5 5 1386526544 5 6 51386526544@user3103155尝试左连接
SELECT
    P.PostID, I.ImageID, I.PostID, P.Post_timestamp
FROM
    Posts P
    INNER JOIN Images I ON P.PostID = I.PostID
GROUP BY
    P.PostID   
ORDER BY
    P.Post_timestamp DESC
LIMIT
    1,5;