0

所以我有一个像这样的查询

SELECT * FROM `catalog` WHERE `id` IN ('2','2','3','3','3');

而这仅返回 2 行,id 为 2 和 3。有可能让它返回 5 行(2 行,id 为“2”,3 行,id 为“3”)或添加计数作为新列?

4

3 回答 3

1

不知道你为什么要做这样的事情,但你可以使用内部查询而不是使用“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-27T23:04:58.943 回答
1

尝试这样的事情:

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-27T23:13:09.053 回答
0

可以使用临时表来完成:

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;
于 2012-10-27T23:18:11.040 回答