MySQL连接多行

时间:2014-12-30 13:16:23

标签: mysql sql select group-by pivot

我有2个表的数据库,1个用于存储客户ID和客户信息。第二个表按键/值排序,因为我需要存储未定义的值。

表格结构

表客户:

=================
    id | status
=================

table customers_info

=======================================
  id | id_customer | key | value
=======================================

内容示例:

表客户:

=================
    id | status
    1  | 1
    2  | 1
==================

table customers_info

=======================================
    id | id_customer | key | value
    1  | 1           | name| Doe
    2  | 1           | age | 25
    3  | 1           | city| NY
    4  | 2           | name| Smith
=======================================

如何查询表格以显示所有客户的姓名

示例:

=================
    1 | Doe
    2 | Smith
=================

如果我只是做一个内连接,我只得到表的第一个键:

SELECT * FROM customers inner join customers_info on customers.id = customers_info.id_customer

8 个答案:

答案 0 :(得分:0)

试试这个:

SELECT ci.id_customer AS CustomerId, ci.value AS CustomerName
FROM customers_info ci 
WHERE ci.key = 'name'
ORDER BY ci.id_customer;

检查SQL FIDDLE DEMO

<强>输出

| CUSTOMERID | CUSTOMERNAME |
|------------|--------------|
|          1 |          Doe |
|          2 |        Smith |

<强> :: EDIT ::

获取客户的所有字段

SELECT ci.id_customer AS CustomerId, 
       MAX(CASE WHEN ci.key = 'name' THEN ci.value ELSE '' END) AS CustomerName, 
       MAX(CASE WHEN ci.key = 'age' THEN ci.value ELSE '' END) AS CustomerAge, 
       MAX(CASE WHEN ci.key = 'city' THEN ci.value ELSE '' END) AS CustomerCity
FROM customers_info ci 
GROUP BY ci.id_customer
ORDER BY ci.id_customer;

检查SQL FIDDLE DEMO

<强>输出

| CUSTOMERID | CUSTOMERNAME | CUSTOMERAGE | CUSTOMERCITY |
|------------|--------------|-------------|--------------|
|          1 |          Doe |          25 |           NY |
|          2 |        Smith |             |              |

<强> :: EDIT ::

从性别表中获取GENDER:

SELECT a.CustomerId, a.CustomerName, a.CustomerAge, a.CustomerCity, g.name AS CustomerGender
FROM (SELECT ci.id_customer AS CustomerId, 
             MAX(CASE WHEN ci.key = 'name' THEN ci.value ELSE '' END) AS CustomerName, 
             MAX(CASE WHEN ci.key = 'age' THEN ci.value ELSE '' END) AS CustomerAge, 
             MAX(CASE WHEN ci.key = 'city' THEN ci.value ELSE '' END) AS CustomerCity
      FROM customers_info ci 
      GROUP BY ci.id_customer
     ) AS a
INNER JOIN customers c ON a.CustomerId = c.id 
LEFT OUTER JOIN gender g ON C.genderId = c.id 
ORDER BY a.CustomerId;

答案 1 :(得分:0)

试试这个:

SELECT c.id as 'customer id', info.value as 'Customer Name'
FROM customers c JOIN customer_info info 
ON c.id = info.id_customer 

答案 2 :(得分:0)

SELECT id_customer, value FROM customers_info WHERE key LIKE 'name'

答案 3 :(得分:0)

使用内连接或连接来交叉结果.. //内连接和连接都是相同的

试试这个

SELECT c.id as 'Id', i.value as 'Name'
FROM customers c INNER JOIN customer_info AS i 
ON c.id = i.id_customer 

// OR

SELECT c.id as 'Id', i.value as 'Name'
FROM customers c INNER JOIN customer_info AS i 
ON c.id = i.id_customer
GROUP BY i.id_customer

答案 4 :(得分:0)

尝试此查询:

select ci.value
from customers_info ci
INNER JOIN customers c 
    ON c.id = ci.id_customer
WHERE ci.key IN ['name'];

答案 5 :(得分:0)

DROP TABLE IF EXISTS eav_hell;

CREATE TABLE eav_hell
(id_customer INT NOT NULL
,attribute VARCHAR(12) NOT NULL
,value VARCHAR(12) NOT NULL
,PRIMARY KEY(id_customer,attribute)
);

INSERT INTO eav_hell VALUES
(1,'name','Doe'),
(1,'age',25),
(1,'city','NY'),
(2,'name','Smith');

SELECT id_customer 
     , MAX(CASE WHEN attribute = 'name' THEN value END) name 
  FROM eav_hell 
 GROUP 
    BY id_customer;
+-------------+-------+
| id_customer | name  |
+-------------+-------+
|           1 | Doe   |
|           2 | Smith |
+-------------+-------+

答案 6 :(得分:0)

select t1.id,t2.key1 from table1 as t1 inner join table2 as t2 on  
t1.id=t2.id_customer group by t2.key1,t1.id  
having t2.key1 in ('name')

SQLFiddle

答案 7 :(得分:0)

不要太复杂。就这么简单:

SELECT C.`id`, CI.`value` from customers_info CI
JOIN customers C ON = CI.`id_customer` = C.`id`
WHERE CI.`key` = 'name'
ORDER BY CI.`id_customer`;