我正在使用PHP MySQLi
为查询准备的语句
$stmt = $dbconnect->prepare("
SELECT COUNT(r.review_id)
, p.postid
, p.title
, p.tags
, p.ltags
, p.status
, p.timestamp
FROM njdb.post_review r
JOIN njdb.post_si p
WHERE r.postid REGEXP ?
AND r.review_status = 'a'
AND p.postid REGEXP ?
AND p.status = 'a';
");
$stmt->bind_param('ss',$search,$search);
$stmt->execute();
$stmt->bind_result($result_post_review, $result_postid, $result_title, $result_hashtags, $result_location, $result_status, $result_timestamp);
$search
是一个值为 id1|id2|id3|id4
的字符串
有2 tables
在这里玩post_si
和 post_review
我希望上面的内容能给我评论总数+帖子的详细信息。但我在 WHERE
中遇到问题区域,因为有些帖子没有任何评论,因此 AND post_review.review_status = 'a'
导致了一些问题。
我认为我真正需要的是使用条件语句,但我还没有在 mysql if no reviews its ok just get the details of the post and move on to the next post
中尝试过。我怎么做?谢谢!
最佳答案
您需要使用LEFT JOIN
来获取没有匹配评论的帖子。您还需要适当的加入条件,将帖子与其评论链接起来。还有一个 GROUP BY
子句用于通过邮寄获取计数,否则您会将所有内容汇总到一个结果中。
SELECT COUNT(r.review_id)
, p.postid
, p.title
, p.tags
, p.ltags
, p.status
, p.timestamp
FROM njdb.post_si p
LEFT JOIN njdb.post_review r ON r.postid = p.postid AND r.review_status = 'a'
WHERE p.postid REGEXP ?
AND p.status = 'a'
GROUP BY p.postid;
关于php - MySQL选择查询内部连接表,有2个针对不同表的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20786896/