如何在单个选择中返回多个值

时间:2017-02-21 11:24:53

标签: mysql row multiple-select

我正在将数据从MySQL转换为Postgres和Postgres数据库电话号码作为单独的字段存储在客户表中,即 Tel_Home Tel_Work Tel_Fax Cellular 但是在MySQL中,电话号码存储在自己的表 telnos 中。

我的问题是如何在单个选择语句中将单个行中返回的表 telnos (可能为Null - 4)中的所有电话号码作为最多4个单独的字段而不是4个单独的记录/行

这是我试图提出的但没有成功,因为每位客户最多可存储4个可能的数字。

    Select A.Name,A.surname,B1.number,B2.number
     from Customer A
    left join telnos B1 on A.telno=B1.id
    left join telnos B2 on A.telno=B2.id and B2.number<>B1.number

我期待结果是这样的

 Firstname, Surname ,Tel1 , Tel2,  Tel3,  Tel4
 =============================================
 Kurt       Hectic   12345  NULL   NULL   NULL
 Clark      Kent     12345  12345  NULL   NULL 
 Peter      Parker   12345  12345  NULL   12345

表Customer和telnos的结构

  Customer  (How I want the result to be stored in new table Customer)
  ========
  Name      varchar(25)
  Surname   varchar(30)
  Tel_Home  varchar(15)
  Tel_Work  varchar(15)
  Tel_Fax   varchar(15)
  Cellular  varchar(15)

   Telnos                            Customer
   ======                            ========
   id          Integer(6)            Id        Integer(6)
   Description varchar(10)           Name      varchar(25)
   Number      varchar(12)           Surname   varchar(30)
                                     telno     Integer(6) FK of telnos

1 个答案:

答案 0 :(得分:0)

你可以使用这样的查询:

SELECT c.*
, GROUP_CONCAT(IF(nr = 0 ,t.`Number`,'') SEPARATOR '') AS tel1
, GROUP_CONCAT(IF(nr = 1 ,t.`Number`,'') SEPARATOR '') AS tel2
, GROUP_CONCAT(IF(nr = 2 ,t.`Number`,'') SEPARATOR '') AS tel3
, GROUP_CONCAT(IF(nr = 3 ,t.`Number`,'') SEPARATOR '') AS tel4
FROM Customer c
LEFT JOIN (
    SELECT t.*
    , @nr:=(@nr+1) 
    ,@nr:=IF(@lid = t.id,@nr,0) AS nr
    ,@lid:=t.id
    FROM telnos t
    CROSS JOIN (SELECT @nr:=0, @lid:=-1) AS INIT
) t ON t.id = c.telno
GROUP BY c.id;

示例表

mysql> select * FROM Customer;
+----+-------+---------+-------+
| id | Name  | Surname | telno |
+----+-------+---------+-------+
|  1 | BERND | BUFFEN  |   123 |
|  2 | JOHN  | DOH     |   124 |
|  3 | PETER | PAN     |   125 |
+----+-------+---------+-------+
3 rows in set (0,00 sec)

mysql> select * from telnos;
+-----+-------------+--------+
| id  | Description | Number |
+-----+-------------+--------+
| 123 | nr1         | 121    |
| 123 | nr2         | 122    |
| 123 | nr3         | 123    |
| 123 | nr4         | 124    |
| 124 | nr1         | 231    |
| 124 | nr2         | 232    |
| 125 | nr1         | 341    |
+-----+-------------+--------+
7 rows in set (0,00 sec)

示例查询

mysql> SELECT c.*
    -> , GROUP_CONCAT(IF(nr = 0 ,t.`Number`,'') SEPARATOR '') AS tel1
    -> , GROUP_CONCAT(IF(nr = 1 ,t.`Number`,'') SEPARATOR '') AS tel2
    -> , GROUP_CONCAT(IF(nr = 2 ,t.`Number`,'') SEPARATOR '') AS tel3
    -> , GROUP_CONCAT(IF(nr = 3 ,t.`Number`,'') SEPARATOR '') AS tel4
    -> FROM Customer c
    -> LEFT JOIN (
    ->     SELECT t.*
    ->     , @nr:=(@nr+1)
    ->     ,@nr:=IF(@lid = t.id,@nr,0) AS nr
    ->     ,@lid:=t.id
    ->     FROM telnos t
    ->     CROSS JOIN (SELECT @nr:=0, @lid:=-1) AS INIT
    -> ) t ON t.id = c.telno
    -> GROUP BY c.id;
+----+-------+---------+-------+------+------+------+------+
| id | Name  | Surname | telno | tel1 | tel2 | tel3 | tel4 |
+----+-------+---------+-------+------+------+------+------+
|  1 | BERND | BUFFEN  |   123 | 121  | 122  | 123  | 124  |
|  2 | JOHN  | DOH     |   124 | 231  | 232  |      |      |
|  3 | PETER | PAN     |   125 | 341  |      |      |      |
+----+-------+---------+-------+------+------+------+------+
3 rows in set (0,00 sec)

mysql>