[英]Count same values in mysql query
So i have a query like 所以我有一个像
SELECT * FROM `catalog` WHERE `id` IN ('2','2','3','3','3');
And this return only 2 rows with id 2 and 3. It is possible make it return 5 rows (2 with id "2" and 3 with id "3") or add count as new column? 并且这只返回2行,ID为2和3.有可能使它返回5行(2个ID为“2”,3个ID为“3”)或者将计数添加为新列?
Not sure why you would want to do something like this, but instead of using an 'in' clause you could use an inner query: 不确定为什么你会想要做这样的事情,但不是使用'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
Try something like this: 尝试这样的事情:
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
It can be done using temporary tables: 可以使用临时表来完成:
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);
if you need count 如果你需要数数
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.