久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

mysql - 數據庫JOIN查詢

瀏覽:137日期:2022-06-13 17:56:36

問題描述

問題解答

回答1:

drop table if exists article;drop table if exists category;drop table if exists r_ac;create table article(id serial not null,title varchar(100),expire timestamp,primary key(id));create table category(id serial not null,name varchar(50),primary key(id));create table r_ac(article int not null,category int not null,primary key(article, category));insert into article(title, expire) values (’a’, ’2017-05-20’),(’b’, null),(’c’, ’2017-03-04’),(’d’, ’2017-02-23’),(’e’, ’2017-04-23’),(’f’, ’2016-09-15’),(’g’, ’2017-06-09’);insert into category(name) values (’c1’),(’c2’),(’c3’),(’c4’),(’c5’),(’c6’),(’c7’);insert into r_ac (article, category) values(1, 1), (1, 2), (1, 5), (1, 7),(2, 1), (2, 6),(3, 5),(4, 1), (4, 4),(7, 1), (7, 7);select category, c.name, count(1) as c from r_ac as acinner join (select id, title, expire from article where expire is null or expire>now()) as z on ac.article=z.idleft join category as c on ac.category=c.idgroup by category, c.name;回答2:

select c.id,count(a.id) from category c LEFT JOIN r_ac r on r.category=c.idLEFT JOIN article a on a.id=r.article and ifnull(a.expire>NOW(),1)GROUP BY c.id

相關文章:
主站蜘蛛池模板: 武陟县| 盖州市| 琼中| 上思县| 山丹县| 嘉义市| 阿鲁科尔沁旗| 涟水县| 东乌珠穆沁旗| 玉龙| 喜德县| 沂南县| 赤壁市| 务川| 龙海市| 象州县| 读书| 常州市| 卢氏县| 洛隆县| 交城县| 安达市| 合江县| 榆林市| 合阳县| 博湖县| 涿州市| 开鲁县| 嘉定区| 汪清县| 清徐县| 河津市| 红桥区| 曲周县| 西吉县| 佛山市| 固阳县| 富民县| 平谷区| 诸暨市| 易门县|