문제

Take a look at the following mySQL query:

SELECT fname,lname FROM users WHERE users.id IN (SELECT sub FROM friends WHERE friends.dom = 1 )

The above query first creates a set of ALL the friends.sub's via the inner query, and then the outer query selects a list of users where user ids are contained within the set created by the inner query (ie the union of the two sets).

And this works fine. But if you needed the inner set to contain not only the subs where dom = 1, but also the doms where sub = 1, like so: Outer query remains same as above, pure pseudocode:

(SELECT sub FROM friends WHERE friends.dom = 1 )
***AND***
(SELECT dom FROM friends WHERE friends.sub = 1 )

Is it possible to make this sort of functionality with the inner query??

Any help or assistance appreciated guys;-D

Thanks a lot guys, my headache is gone now!

도움이 되었습니까?

해결책

Try this:

SELECT u.fname, u.lname
FROM users u
INNER JOIN friends f
  ON (u.id = f.sub AND f.dom = 1)
  OR (u.id = f.dom AND f.sub = 1)

다른 팁

I'm not sure if I correctly understand what sub and dom represent, but it looks like you can use a UNION in there:

SELECT fname, lname 
FROM   users 
WHERE  users.id IN 
       (  
          SELECT sub FROM friends WHERE friends.dom = 1 
          UNION
          SELECT dom FROM friends WHERE friends.sub = 1 
       );

Test case:

CREATE TABLE users (id int, fname varchar(10), lname varchar(10));
CREATE TABLE friends (dom int, sub int);

INSERT INTO users VALUES (1, 'Bob', 'Smith');
INSERT INTO users VALUES (2, 'Peter', 'Brown');
INSERT INTO users VALUES (3, 'Jack', 'Green');
INSERT INTO users VALUES (4, 'Kevin', 'Jackson');
INSERT INTO users VALUES (5, 'Steven', 'Black');

INSERT INTO friends VALUES (1, 2);
INSERT INTO friends VALUES (1, 3);
INSERT INTO friends VALUES (4, 1);
INSERT INTO friends VALUES (3, 4);
INSERT INTO friends VALUES (5, 2);

Result:

+-------+---------+
| fname | lname   |
+-------+---------+
| Peter | Brown   |
| Jack  | Green   |
| Kevin | Jackson |
+-------+---------+
3 rows in set (0.00 sec)

That said, @Alec's solution is probably more efficient.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top