简体   繁体   English

MySQL创建临时表并获取过滤的数据

[英]Mysql create temporary table and fetch filtered data

I have a the following MySQL tables (MySQL 5.6): 我有以下MySQL表(MySQL 5.6):

contacts 往来

+----+----------------+
| id |     email      |
+----+----------------+
|  1 | one@domain.tld |
|  2 | two@domain.tld |
+----+----------------+

contact_attributes contact_attributes

+----+-------------+
| id |    name     |
+----+-------------+
|  1 | name        |
|  2 | current_car |
|  3 | car_color   |
+----+-------------+

contact_attribute_values contact_attribute_values

+----+------------+-------------------------+------------+
| id |   value    | contact_attribute_value | contact_id |
+----+------------+-------------------------+------------+
|  1 | John Doe   |                       1 |          1 |
|  2 | Eva Milton |                       1 |          2 |
|  3 | BMW X3     |                       2 |          1 |
|  4 | Volvo XC90 |                       2 |          2 |
|  5 | Pink       |                       3 |          1 |
|  6 | Blue       |                       3 |          2 |
+----+------------+-------------------------+------------+

Is there a way to create a temporary table with the following schema in MySQL to fetch and filter the contacts by attributes? 有没有一种方法可以在MySQL中使用以下架构创建临时表,以按属性获取和过滤联系人?

+------------+----------------+------------+-------------+-----------+
| contact_id | contact_email  |    name    | current_car | car_color |
+------------+----------------+------------+-------------+-----------+
|          1 | one@domain.tld | John Doe   | BMW X3      | Pink      |
|          2 | two@domain.tld | Eva Milton | Volvo XC90  | blue      |
+------------+----------------+------------+-------------+-----------+

Thanks for your help 谢谢你的帮助

You could create a view which generates this result: 您可以创建一个生成以下结果的视图:

CREATE VIEW contacts_view AS
SELECT
    c.id,
    c.email,     -- OK to select this since we GROUP BY the primary key
    MAX(CASE WHEN ca.name = 'name'        THEN cav.value END) AS name,
    MAX(CASE WHEN ca.name = 'current_car' THEN cav.value END) AS current_car,
    MAX(CASE WHEN ca.name = 'car_color'   THEN cav.value END) AS car_color
FROM contacts c
LEFT JOIN contact_attribute_values cav
    ON c.id = cav.contact_id
INNER JOIN contact_attributes ca
    ON cav.contact_attribute_value = ca.id
GROUP BY
    c.id;

SQLFiddle SQLFiddle

You could create a temporary table which has the output from the above view. 可以创建一个临时表,该表具有上述视图的输出。 But I sense that a view is more of what you are after here. 但是我觉得,这里的景色更多地是您追求的。 A temporary table would be deleted after your session ends, which means you have to populate again each time. 会话结束后,将删除一个临时表,这意味着您每次必须重新填充。 And, the data inside would get stale as soon as the data in your underlying tables changes. 并且,只要基础表中的数据发生更改,内部数据就会过时。

Use CASE expression like this: 使用CASE表达式,如下所示:

SELECT
  v.contact_id,
  MAX(CASE WHEN a.name = 'contact_email' THEN v.value ELSE '' END) AS 'contact_email',
  MAX(CASE WHEN a.name = 'name' THEN v.value ELSE '' END) AS 'name',
  MAX(CASE WHEN a.name = 'current_car' THEN v.value ELSE '' END) AS 'current_car'
  MAX(CASE WHEN a.name = 'car_color' THEN v.value ELSE '' END) AS 'car_color'
FROM contacts as c
INNER JOIN contact_attribute_values as v on c.id = v.contact_id
INNER JOIN contact_attributes as a on v.contact_attribute_value = a.id
group by v.contact_id;

And you don't have to write down all the attributes, you can make it dynamically like this: 而且您不必写下所有属性,可以像这样动态创建它:

SET @cols = NULL;
SET @sql = NULL;

SELECT
  GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(a.name = ''',
      a.name, ''', v.value , NULL)) AS ', '''', a.name , '''')
  ) INTO @cols
FROM contact_attributes a;


SET @sql = CONCAT('SELECT 
  v.contact_id,', @cols, '
    FROM contacts as c
LEFT JOIN contact_attribute_values as v on c.id = v.contact_id
INNER JOIN contact_attributes as a on v.contact_attribute_value = a.id
group by v.contact_id;');

prepare stmt 
FROM @sql;

execute stmt;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM