SELECT tbl_post_upvote.id AS i ,
COUNT(user_id) AS c
FROM tbl_post_upvote
LEFT JOIN tbl_post ON (tbl_post.id=tbl_post_upvote.post_id)
WHERE tbl_post.author_id = 3 and is_seen = 0
UNION
SELECT tbl_post_downvote.id AS i,
COUNT(user_id)
FROM tbl_post_downvote
LEFT JOIN tbl_post ON (tbl_post.id=tbl_post_downvote.post_id)
WHERE tbl_post.author_id=3 and is_seen=0
$r_noti = mysql_query($sq_noti);
$l = mysql_fetch_assoc($r_noti);
$l_noti = mysql_num_rows($r_noti);
if ($l['c'] > 0 && $l['i']!=0) { ?>
<span class="badge">
<?= $l_noti; ?>
</span>
<?php } ?>
显示结果 我的值为 6 , 0 和c值分别为1、0。 我想删除 0 ,0 行 结果仅为 i 值 6 和 c 值 1
最佳答案
您必须将 GROUP BY i
添加到您的语句中。
请尝试以下操作:
select tbl_post_upvote.id as i ,count(user_id) as c
from tbl_post_upvote left join
tbl_post
on (tbl_post.id=tbl_post_upvote.post_id)
where tbl_post.author_id=3 and is_seen=0
group by i
union
select tbl_post_downvote.id as i,count(user_id)
from tbl_post_downvote left join
tbl_post
on (tbl_post.id=tbl_post_downvote.post_id)
where tbl_post.author_id=3 and is_seen=0
group by i
关于php - mysql 查询 union 和 mysql_num_rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32036760/