假设表中存在等于1和3的fk
,则以下查询将返回两个记录。
SELECT fk, name FROM my_table WHERE fk IN (1,3,1);
我怎样才能返回三条记录:
fk name
1 john
3 mary
1 john
答案 0 :(得分:0)
您需要使用join
代替:
select t.fk, t.name
from my_table t join
(select 1 as x union all select 3 union all select 1
) vals
on t.fk = vals.x;
如果您还想保留订单,请使用第二列 - 除非您添加join
,否则无法保证order by
之后行的顺序:
select t.fk, t.name
from my_table t join
(select 1 as x, 1 as ordering union all
select 3, 2 union all
select 1, 3
) vals
on t.fk = vals.x
order by vals.ordering;