[英]Count same values in mysql query
所以我有一个像
SELECT * FROM `catalog` WHERE `id` IN ('2','2','3','3','3');
并且这只返回2行,ID为2和3.有可能使它返回5行(2个ID为“2”,3个ID为“3”)或者将计数添加为新列?
不确定为什么你会想要做这样的事情,但不是使用'in'子句,你可以使用内部查询:
select *
from `catalog` c,
(
select 2 ids
union all
select 2
union all
select 3
union all
select 3
union all
select 3
) k
where c.id = k.ids
尝试这样的事情:
SELECT t.p,count(*) FROM
catalog,
(SELECT 2 as id
Union all select 2 as id
Union all select 3 as id
Union all select 3 as id
Union all select 3 as id)as t
where catalog.id = t.id
可以使用临时表来完成:
create temporary table arrayt (id int);
insert into arrayt values ('2'),('2'),('3'),('3'),('3');
select catalog.* from arrayt a LEFT JOIN catalog on (a.id=catalog.id);
如果你需要数数
select count(catalog.id) as count,catalog.id as id from arrayt a LEFT JOIN catalog on (a.id=catalog.id) group by catalog.id;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.