关于sql语句sum查询的经典分析
在写好的sql语句里该怎么添加条件:sql:SELECTsum((casewhenaddress.idisnotnullthen1else0end))num,art.id...
在写好的sql语句里该怎么添加条件:
sql:
SELECT
sum((case when address.id is not null then 1 else 0 end)) num,
art.id,
art.admin_id kehu_id,
art.news_title ,
address.id addrid,
address.admin_id,
admin.change_time
FROM
tb_news_article art
LEFT JOIN
tb_news_article_address address
ON
art.id = address.news_article_id
LEFT JOIN
tb_admin_news admin
ON
art.id = admin.news_article_id
WHERE
(admin.admin_id = 45)
GROUP BY
art.id 展开
sql:
SELECT
sum((case when address.id is not null then 1 else 0 end)) num,
art.id,
art.admin_id kehu_id,
art.news_title ,
address.id addrid,
address.admin_id,
admin.change_time
FROM
tb_news_article art
LEFT JOIN
tb_news_article_address address
ON
art.id = address.news_article_id
LEFT JOIN
tb_admin_news admin
ON
art.id = admin.news_article_id
WHERE
(admin.admin_id = 45)
GROUP BY
art.id 展开
2个回答
展开全部
--sql:
SELECT
sum((case when address.id is not null then 1 else 0 end)) num,
art.id,
art.admin_id kehu_id,
art.news_title ,
address.id addrid,
address.admin_id,
admin.change_time
FROM tb_news_article art
LEFT JOIN tb_news_article_address address ON art.id = address.news_article_id
LEFT JOIN tb_admin_news admin ON art.id = admin.news_article_id
WHERE (admin.admin_id = 45)
GROUP BY art.id
having num>5
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询