dongxuxian1123 2015-09-27 23:24
浏览 30
已采纳

算一个非常复杂的选择mysql

I have a very complex select that union 3 tables to show customer's last activities.

so:

select c.data, p.user, concat(c.user, ' buy your product') as logs from sell c inner join posts p on c.foto = p.id where p.user = ?
union all
select f.data, c.user, concat(c.user, ' asked you a question') as logs from questions f inner join cadastro c ON f.user = c.id where f.nick = ?
union all
select l.data, l.user, concat(l.user, ' like your product') as logs from likes l inner join posts p on l.post = p.id where p.user = ?
order by data desc

well, each of this tables has a row called SEEN. it is a varchar set with 0 or 1 (0 the user has not seen the message, 1 the user has already seen it).

What I want to add in this query is something to count the rows where seen = 0.

I want to show: YOU HAVE 4 new alerts.

select count(*) will not work and will not count right (the seen set to 1).

What can I do to count all this selects where seen = 0?

  • 写回答

1条回答 默认 最新

  • doudou3213 2015-09-27 23:51
    关注

    Try this

    select a.*, SUM(IF(a.seen='0', 1, 0)) as 'unseen_count' FROM (
        select c.data, p.user, concat(c.user, ' buy your product') as logs, seen from sell c inner join posts p on c.foto = p.id where p.user = ?
        union all
        select f.data, c.user, concat(c.user, ' asked you a question') as logs, seen from questions f inner join cadastro c ON f.user = c.id where f.nick = ?
        union all
        select l.data, l.user, concat(l.user, ' like your product') as logs, seen from likes l inner join posts p on l.post = p.id where p.user = ?
    ) as a
    order by data desc
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部