[英]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.