列出所引用表中所有关联者的姓名?

时间:2015-03-29 09:53:06

标签: mysql sql

我有一张表test,如下所示:

+-------+-------+
| u1_id | u2_id |
+-------+-------+
|     1 |     2 |
|     3 |     1 |
|     2 |     1 |
|     2 |     3 |
+-------+-------+

并且,u1_idu2_id都是'外键'到另一个表user

+----+-------+
| id | name  |
+----+-------+
|  1 | n_foo |
|  2 | n_bar |
|  3 | n_baz |
+----+-------+

不确定如何解释,但是:

  • 在输入中,我有一个用户id,可以在u1_idu2_id中引用。
  • 我希望使用表test上的联接来获取表user中定义的关联用户。

对于用户id = 1,我应该:

n_bar
n_baz
n_bar

对于用户id = 2,我应该:

n_foo
n_foo
n_baz

这可能是一个常见问题,但没有找到如何使用以下方式加入这两个表:

  • u1_id如果我的输入用户id位于u2_id
  • u2_id否则

我尝试过这样的事情,但似乎没有效果:

SELECT name 
FROM test 
   JOIN user 
   ON user.id = test.u1_id 
WHERE test.u1_id = @guid OR 
      test.u2_id = @guid AND 
      CASE 
         WHEN test.u2_id = @guid 
         THEN test.u2_id = test.u1_id 
      END;

任何想法如何实现这一目标?或者可能有更好的方法来设计这些表格,我完全接受任何建议。

3 个答案:

答案 0 :(得分:1)

怎么样:

SELECT IF(u1.id = @guid, u2.name, u1.name) AS name
FROM test 
JOIN user u1 ON u1.id = test.u1_id 
JOIN user u2 ON u2.id = test.u2_id 
WHERE test.u1_id=@guid OR test.u2_id=@guid;

答案 1 :(得分:1)

如果我正确理解了您的问题,我相信您需要以下查询:

SELECT t2.`name` 
FROM `t2`
INNER JOIN (
    SELECT IF(@uid = 1, t1.u1_id, t1.u2_id) as `id`
    FROM `t1`
) as `t1`
WHERE t2.id = t1.id and t1.id != @uid;

我试过以下:

日期基础创建,我不知道您正在使用哪些列类型进行演示:

create table t1 (
    u1_id int,
    u2_id int
);
insert into t1 values 
(1,    2),
(3,    1),
(2,    1),
(2,    3);

create table t2 (
    id int,
    name varchar(10)
);
insert into t2 values 
(  1 , 'n_foo' ),
(  2 , 'n_bar' ),
(  3 , 'n_baz' );

然后查询:

mysql> SELECT * FROM t1;
+-------+-------+
| u1_id | u2_id |
+-------+-------+
|     1 |     2 |
|     3 |     1 |
|     2 |     1 |
|     2 |     3 |
+-------+-------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM t2;
+------+-------+
| id   | name  |
+------+-------+
|    1 | n_foo |
|    2 | n_bar |
|    3 | n_baz |
+------+-------+
3 rows in set (0.00 sec)

mysql> SET @uid = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @uid;
+------+
| @uid |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> SELECT t2.`name` 
    -> FROM `t2`
    -> INNER JOIN (
    ->     SELECT IF(@uid = 1, t1.u1_id, t1.u2_id) as `id`
    ->     FROM `t1`
    -> ) as `t1`
    -> WHERE t2.id = t1.id and t1.id != @uid;
+-------+
| name  |
+-------+
| n_baz |
| n_bar |
| n_bar |
+-------+
3 rows in set (0.03 sec)

mysql> SET @uid = 2;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @uid;
+------+
| @uid |
+------+
|    2 |
+------+
1 row in set (0.00 sec)

mysql> SELECT t2.`name` 
    -> FROM `t2`
    -> INNER JOIN (
    ->     SELECT IF(@uid = 1, t1.u1_id, t1.u2_id) as `id`
    ->     FROM `t1`
    -> ) as `t1`
    -> WHERE t2.id = t1.id and t1.id != @uid;
+-------+
| name  |
+-------+
| n_foo |
| n_foo |
| n_baz |
+-------+
3 rows in set (0.00 sec)

顺便说一句,如果不是你想要的,你可以改变连接条件。但是因为它给出了正确的结果......

试一试!!

答案 2 :(得分:1)

使用@ GrijeshChauhan的架构......

SELECT * FROM
(SELECT u1_id,u2_id FROM t1
 UNION ALL
 SELECT u2_id,u1_id FROM t1
) x
JOIN t2
  ON t2.id = x.u1_id
WHERE x.u2_id = 2;
+-------+-------+------+-------+
| u1_id | u2_id | id   | name  |
+-------+-------+------+-------+
|     1 |     2 |    1 | n_foo |
|     1 |     2 |    1 | n_foo |
|     3 |     2 |    3 | n_baz |
+-------+-------+------+-------+