所以我有一个查询,比如
SELECT * FROM `catalog` WHERE `id` IN ('2','2','3','3','3');可以让它返回5行(2行,id为"2“,3行,id为"3")或添加count作为新列?
发布于 2012-10-28 07:04:59
不知道为什么要这样做,但可以使用内部查询而不是'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发布于 2012-10-28 07:13:09
尝试如下所示:
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发布于 2012-10-28 07:18:11
可以使用临时表完成此操作:
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;https://stackoverflow.com/questions/13104618
复制相似问题