简体   繁体   中英

Sql query to select different row with same ID

I have a simple table and I wanted to get a row with same ID but different values.

lets assume I don't know the IDs;

可以说我有;

I have feature "one" and I want to find the feature that has same ID with feature "one".

I'm going to go with 'funky';

SELECT y.*
  FROM my_table x
  JOIN my_table y
    ON y.id = x.id 
   AND y.feature <> x.feature
 WHERE x.feature = 'one';

As Strawberry commented, you could use an inner join:

SELECT f2.feature
FROM feature f1
INNER JOIN feature f2 ON f1.id = f2.id AND f1.feature <> f2.feature
WHERE f1.feature='one'

One way to do this is with the exists operator:

SELECT *
FROM   features f_outer
WHERE  EXISTS (SELECT *
               FROM   features f_inner
               WHERE  f_outer.id = f_inner.id AND
                      f_outer.feature != f_inner.feature AND
                      f_inner.feature = 'one')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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