如何在mysql中显示ID

时间:2019-01-22 12:24:16

标签: mysql

所以我有一个带有ID和名称的表。 我有

的where条件
name = 'John'
name = 'Ariel'
name = 'Ariel'
name = 'Mary'
name = 'Mary'

我想显示它的ID。所以输出应该是这样的:

id  name
1   John 
1   John
3   Ariel
3   Ariel
5   Mary
5   Mary

我所做的就是这样:

select id, name from users where
name = 'John' OR
name = 'John' OR
name = 'Ariel' OR
name = 'Ariel' OR
name = 'Mary' OR
name = 'Mary';

也尝试过

where name in ('John','John','Ariel','Ariel','Mary','Mary');

但是它总是这样输出:

id  name
1   John
3   Ariel
5   Mary

那么即使条件重复,如何显示ID? 我真的很想展示这样的东西:

id  name
1   John 
1   John
3   Ariel
3   Ariel
5   Mary
5   Mary

或者这甚至有可能吗?

注意:我没有重复的记录。

1 个答案:

答案 0 :(得分:2)

您可以尝试在suqyery上使用基于内部所有联接的内部联接

select m.id, m.name 
from my_table  m 
inner join (
select 'John' as name  from dual 
union all 
select 'John' from dual 
union all 
select 'Ariel' from dual 
union all 
select 'Ariel' from dual 
union all 
select 'Mary' from dual 
union all 
select 'Mary' from dual 
) t on t.name =m.name