选择一个单元格,但其他一切

时间:2014-03-25 05:10:09

标签: mysql sql

我有两张桌子:

      groups                  group_members
    v-------------------------------v
 +----+------------+     +----+----------+------+
 | id | group_name |     | id | group_id | name |
 +----+------------+     +----+----------+------+
 | 1  | admin      |     | 1  | 1        | Dave |
 +----+------------+     +----+----------+------+
 | 2  | moderator  |     | 2  | 1        | Bob  |
 +----+------------+     +----+----------+------+
 | 3  | vip        |     | 3  | 2        | Sue  |
 +----+------------+     +----+----------+------+
 | 4  | user       |     | 4  | 2        | Jill |
 +----+------------+     +----+----------+------+
                         | 5  | 2        | Jack |
                         +----+----------+------+
                         | 6  | 3        | Bill |
                         +----+----------+------+
                         | 7  | 4        | Sam  |
                         +----+----------+------+

group_membersgroup_id中有idgroups的外键引用。

我想获取在群组中找到的成员名单,但也要在此过程中返回group_name。我想只在一个查询中执行此操作。

我已尝试此查询,但它返回每行的组名称,我希望将group_name视为自己的行。

SELECT groups.group_name, 
       group_members.name 
FROM   groups, 
       group_members 
WHERE  group_members.group_id = groups.id 
       AND groups.id = 1 

结果是:

+-------------------+--------------------+
| groups.group_name | group_members.name |
+-------------------+--------------------+
| admin             | Dave               |
+-------------------+--------------------+
| admin             | Bob                |
+-------------------+--------------------+

我想看看:

  either this |                     or this |
              v                             v
+-------------------+            +---------------------+
| groups.group_name |            | groups_members.name |
+-------------------+            +---------------------+
| admin             |            | admin               |
+-------------------+            +---------------------+
                                 | Dave                |
+---------------------+          +---------------------+
| groups_members.name |          | Bob                 |
+---------------------+          +---------------------+
| Dave                |
+---------------------+
| Bob                 |
+---------------------+

这可能吗?

3 个答案:

答案 0 :(得分:1)

SELECT if(gm.name='',g.name,gm.name) as name
FROM   groups g
       join (select group_id, name from group_members gm
                  union all
                  select 1 as group_id, '' as name) gm on
       gm.group_id = g.id 
WHERE
       groups.id = 1 

我们可以再添加一个人工行,并选择组名称(对于附加行)或组成员名称

答案 1 :(得分:0)

SELECT groups.group_name      
FROM   groups, 
       group_members 
WHERE  group_members.group_id = groups.id 
       AND groups.id = 1 

UNION

SELECT group_members.name 
FROM   groups, 
       group_members 
WHERE  group_members.group_id = groups.id 
       AND groups.id = 1 

答案 2 :(得分:0)

这一个?

SELECT groups.group_name AS Name
FROM   groups JOIN group_members ON
               group_members.group_id = groups.id 
WHERE groups.id = 1 
UNION
SELECT group_members.name AS Name
FROM   group_members JOIN groups ON
               group_members.group_id = groups.id 
WHERE groups.id = 1