简体   繁体   English

如何从以下场景自连接表?

[英]How to self joining table from following scenario?

Consider I have a table named table1 which contains following data:考虑我有一个名为 table1 的表,其中包含以下数据:

id, value
1, a
1, b
1, c
1, d
2, a
2, b
2, c
3, b
3, c
4, a
4, b
4, c
4, d

I wanted to select all ids that have a value = d.我想选择所有值为 d 的 ID。 How could I achieve this?我怎么能做到这一点?

Which means I need to get the result of ( 1 and 4 ) , since id 2 and 3 do not contain d.这意味着我需要得到 ( 1 和 4 ) 的结果,因为 id 2 和 3 不包含 d。

the actual table contains more than 10,000,000 rows, so the query should not be too slow.实际表包含超过 10,000,000 行,因此查询不应太慢。

Thanks for your ideas.谢谢你的想法。

This should be pretty straight forward.这应该很简单。 ( There is no need for join ) 不需要加入

SELECT *
FROM   TableName
WHERE  value = 'd'

You don't need a join at all:您根本不需要加入:

SELECT id
FROM myTable
WHERE value = 'd'

Try this尝试这个

select id
from Table1
where value= 'd'

Please Try this query it will help you请试试这个查询它会帮助你

select * from table1 t, table1 t1 
where t.value = t1.value and t1.value='d'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM