Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 将查询和行合并到列_Mysql - Fatal编程技术网

Mysql 将查询和行合并到列

Mysql 将查询和行合并到列,mysql,Mysql,我有下面的查询,它获取我需要的行的ID SELECT `wp_posts`.`ID`, `wp_posts`.`post_modified_gmt`, `wp_term_relationships`.`term_taxonomy_id` FROM `wp_posts` LEFT JOIN `wp_term_relationships` ON `wp_posts`.`ID` = `wp_term_relationships`.`object_id` WHERE `wp_posts`.`post_t

我有下面的查询,它获取我需要的行的ID

SELECT `wp_posts`.`ID`, `wp_posts`.`post_modified_gmt`, `wp_term_relationships`.`term_taxonomy_id`
FROM `wp_posts`
LEFT JOIN `wp_term_relationships` ON
`wp_posts`.`ID` = `wp_term_relationships`.`object_id`
WHERE `wp_posts`.`post_type` = 'shop_order'
AND `wp_term_relationships`.`term_taxonomy_id` = 14
AND `wp_posts`.`post_modified` > '2013-04-25 16:02:00'
ORDER BY `wp_posts`.`post_modified` ASC;

+----+---------------------+------------------+
| ID | post_modified       | term_taxonomy_id |
+----+---------------------+------------------+
| 41 | 2013-04-25 16:02:43 |               14 |
| 43 | 2013-04-25 18:40:37 |               14 |
+----+---------------------+------------------+
2 rows in set (0.00 sec)
然后,我循环遍历所有ID,并使用该ID运行下面的查询,以匹配查询中的post\u ID

SELECT `wp_postmeta`.`post_id`, `wp_postmeta`.`meta_key`, `wp_postmeta`.`meta_value` 
FROM `wp_postmeta` 
WHERE `post_id` = 43 # ID from last query
AND (`wp_postmeta`.`meta_key` = '_shipping_first_name'  
OR `wp_postmeta`.`meta_key` = '_shipping_last_name'
OR `wp_postmeta`.`meta_key` = '_shipping_address_1'
OR `wp_postmeta`.`meta_key` = '_shipping_address_2'
OR `wp_postmeta`.`meta_key` = '_shipping_city'
OR `wp_postmeta`.`meta_key` = '_shipping_state'
OR `wp_postmeta`.`meta_key` = '_shipping_postcode'
OR `wp_postmeta`.`meta_key` = '_shipping_country'
OR `wp_postmeta`.`meta_key` = '_order_total'
OR `wp_postmeta`.`meta_key` = '_order_shipping'
OR `wp_postmeta`.`meta_key` = '_order_discount'
OR `wp_postmeta`.`meta_key` = '_cart_discount'
OR `wp_postmeta`.`meta_key` = '_order_tax'
OR `wp_postmeta`.`meta_key` = '_order_shipping_tax');

+---------+----------------------+------------------+
| post_id | meta_key             | meta_value       |
+---------+----------------------+------------------+
|      43 | _shipping_country    | GB               |
|      43 | _shipping_first_name | Joe              |
|      43 | _shipping_last_name  | Bloggs           |
|      43 | _shipping_address_1  | 18 Street Name   |
|      43 | _shipping_address_2  |                  |
|      43 | _shipping_city       | Manchester       |
|      43 | _shipping_state      | Lancashire       |
|      43 | _shipping_postcode   | MM1 1MM          |
|      43 | _order_shipping      | 0.00             |
|      43 | _order_discount      | 0.00             |
|      43 | _cart_discount       | 0.00             |
|      43 | _order_tax           | 0.00             |
|      43 | _order_shipping_tax  | 0.00             |
|      43 | _order_total         | 224.00           |
+---------+----------------------+------------------+
14 rows in set (0.00 sec)
我想知道是否有办法合并这些查询,所以我将服务器上的查询数量减半

我不确定是否可以这样做,但我想将中的meta_键值更改为列的名称,并使用meta_值作为值

最终结果示例

+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
| ID | post_modified       | term_taxonomy_id | _shipping_country | _shipping_first_name | _shipping_last_name | _shipping_address_1 | _shipping_address_2 | _shipping_city | _shipping_state | _shipping_postcode | _order_shipping | _order_discount | _cart_discount | _order_tax | _order_shipping_tax | _order_total |
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
| 43 | 2013-04-25 18:40:37 |               14 | GB                | Joe                  | Bloggs              | 18 Street Name      |                     | Manchester     | Lancashire      | MM1 1MM            | 0.00            | 0.00            | 0.00           | 0.00       | 0.00                | 224.00       |
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
Next row...
Next row...
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
| ID | post_modified       | term_taxonomy_id | _shipping_country | _shipping_first_name | _shipping_last_name | _shipping_address_1 | _shipping_address_2 | _shipping_city | _shipping_state | _shipping_postcode | _order_shipping | _order_discount | _cart_discount | _order_tax | _order_shipping_tax | _order_total |
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
| 43 | 2013-04-25 18:40:37 |               14 | GB                | Joe                  | Bloggs              | 18 Street Name      |                     | Manchester     | Lancashire      | MM1 1MM            | 0.00            | 0.00            | 0.00           | 0.00       | 0.00                | 224.00       |
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
Next row...
Next row...
任何帮助都会很好

谢谢

编辑

在meewoK的帮助下,这是一个适合我的问题

SELECT (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_first_name' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_shipping_first_name',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_last_name' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_shipping_last_name',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_address_1' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_shipping_address_1',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_address_2' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_shipping_address_2',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_city' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_shipping_city',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_state' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_shipping_state',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_postcode' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_shipping_postcode',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_country' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_shipping_country',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_order_total' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_order_total',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_order_shipping' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_order_shipping',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_order_discount' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_order_discount',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_cart_discount' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_cart_discount',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_order_tax' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_order_tax',
    (CASE WHEN `wp_postmeta`.`meta_key` =  '_order_shipping_tax' THEN `wp_postmeta`.`meta_value` ELSE NULL END) AS '_order_shipping_tax',
    `wp_posts`.`ID`,
    `wp_posts`.`post_modified_gmt`,
    `wp_term_relationships`.`term_taxonomy_id`
FROM `wp_postmeta`, `wp_posts`
LEFT JOIN `wp_term_relationships` ON `wp_posts`.`ID` = `wp_term_relationships`.`object_id`
WHERE `wp_posts`.`post_type` = 'shop_order'
    AND `wp_term_relationships`.`term_taxonomy_id` = 14
    AND `wp_posts`.`post_modified` > '2013-04-25 16:02:00'
    AND `post_id` = `wp_posts`.`ID`
    AND (`wp_postmeta`.`meta_key` = '_shipping_first_name'
        OR `wp_postmeta`.`meta_key` = '_shipping_last_name'
        OR `wp_postmeta`.`meta_key` = '_shipping_address_1'
        OR `wp_postmeta`.`meta_key` = '_shipping_address_2'
        OR `wp_postmeta`.`meta_key` = '_shipping_city'
        OR `wp_postmeta`.`meta_key` = '_shipping_state'
        OR `wp_postmeta`.`meta_key` = '_shipping_postcode'
        OR `wp_postmeta`.`meta_key` = '_shipping_country'
        OR `wp_postmeta`.`meta_key` = '_order_total'
        OR `wp_postmeta`.`meta_key` = '_order_shipping'
        OR `wp_postmeta`.`meta_key` = '_order_discount'
        OR `wp_postmeta`.`meta_key` = '_cart_discount'
        OR `wp_postmeta`.`meta_key` = '_order_tax'
        OR `wp_postmeta`.`meta_key` = '_order_shipping_tax')
GROUP BY `wp_posts`.`ID`
ORDER BY `wp_posts`.`post_modified` ASC;
最终结果示例

+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
| ID | post_modified       | term_taxonomy_id | _shipping_country | _shipping_first_name | _shipping_last_name | _shipping_address_1 | _shipping_address_2 | _shipping_city | _shipping_state | _shipping_postcode | _order_shipping | _order_discount | _cart_discount | _order_tax | _order_shipping_tax | _order_total |
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
| 43 | 2013-04-25 18:40:37 |               14 | GB                | Joe                  | Bloggs              | 18 Street Name      |                     | Manchester     | Lancashire      | MM1 1MM            | 0.00            | 0.00            | 0.00           | 0.00       | 0.00                | 224.00       |
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
Next row...
Next row...
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
| ID | post_modified       | term_taxonomy_id | _shipping_country | _shipping_first_name | _shipping_last_name | _shipping_address_1 | _shipping_address_2 | _shipping_city | _shipping_state | _shipping_postcode | _order_shipping | _order_discount | _cart_discount | _order_tax | _order_shipping_tax | _order_total |
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
| 43 | 2013-04-25 18:40:37 |               14 | GB                | Joe                  | Bloggs              | 18 Street Name      |                     | Manchester     | Lancashire      | MM1 1MM            | 0.00            | 0.00            | 0.00           | 0.00       | 0.00                | 224.00       |
+----+---------------------+------------------+-------------------+----------------------+---------------------+---------------------+---------------------+----------------+-----------------+--------------------+-----------------+-----------------+----------------+------------+---------------------+--------------+
Next row...
Next row...
您可以尝试以下方法:

SELECT `wp_postmeta`.`post_id`, `wp_postmeta`.`meta_key`, 
    `wp_postmeta`.`meta_value` 
FROM `wp_postmeta` 
WHERE `post_id` in (
    SELECT `wp_posts`.`ID`
    FROM `wp_posts`
    LEFT JOIN `wp_term_relationships` ON
        `wp_posts`.`ID` = `wp_term_relationships`.`object_id`
    WHERE `wp_posts`.`post_type` = 'shop_order'
    AND `wp_term_relationships`.`term_taxonomy_id` = 14
    AND `wp_posts`.`post_modified` > '2013-04-25 16:02:00'
    ORDER BY `wp_posts`.`post_modified` ASC)
AND `wp_postmeta`.`meta_key` in ('_shipping_first_name', '_shipping_last_name'
    '_shipping_address_1', '_shipping_address_2', '_shipping_city'
    '_shipping_state', '_shipping_postcode', '_shipping_country'
    '_order_total', '_order_shipping', '_order_discount'
    '_cart_discount', '_order_tax', '_order_shipping_tax');

这需要一个联接才能在一个查询中工作。 大概是这样的:

  SELECT `wp_posts`.`ID`,
       `wp_posts`.`post_modified_gmt`,
       `wp_term_relationships`.`term_taxonomy_id`,
       `wp_postmeta`.`post_id`,
       `wp_postmeta`.`meta_key`,
       `wp_postmeta`.`meta_value`
FROM `wp_postmeta`, `wp_posts`
LEFT JOIN `wp_term_relationships` ON `wp_posts`.`ID` = `wp_term_relationships`.`object_id`
WHERE `wp_posts`.`post_type` = 'shop_order'
  AND `wp_term_relationships`.`term_taxonomy_id` = 14
  AND `wp_posts`.`post_modified` > '2013-04-25 16:02:00'
  AND `post_id` = `wp_posts`.`ID`
  AND (`wp_postmeta`.`meta_key` = '_shipping_first_name'
       OR `wp_postmeta`.`meta_key` = '_shipping_last_name'
       OR `wp_postmeta`.`meta_key` = '_shipping_address_1'
       OR `wp_postmeta`.`meta_key` = '_shipping_address_2'
       OR `wp_postmeta`.`meta_key` = '_shipping_city'
       OR `wp_postmeta`.`meta_key` = '_shipping_state'
       OR `wp_postmeta`.`meta_key` = '_shipping_postcode'
       OR `wp_postmeta`.`meta_key` = '_shipping_country'
       OR `wp_postmeta`.`meta_key` = '_order_total'
       OR `wp_postmeta`.`meta_key` = '_order_shipping'
       OR `wp_postmeta`.`meta_key` = '_order_discount'
       OR `wp_postmeta`.`meta_key` = '_cart_discount'
       OR `wp_postmeta`.`meta_key` = '_order_tax'
       OR `wp_postmeta`.`meta_key` = '_order_shipping_tax')
ORDER BY `wp_posts`.`post_modified` ASC;
编辑:

关于旋转的要求,我将使用以下方法。你必须为每一个可能的meta_键硬编码一个案例

select (CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_first_name' THEN `wp_postmeta`.`meta_key` ELSE NULL END) AS '_shipping_first_name',
(CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_address_1' THEN `wp_postmeta`.`meta_key` ELSE NULL END) AS '_shipping_address_1',
(CASE WHEN `wp_postmeta`.`meta_key` =  '_shipping_address_2' THEN `wp_postmeta`.`meta_key` ELSE NULL END) AS '_shipping_address_2',
...

依此类推。

不工作得到错误,我想要它,这样我就没有第一个查询中的ID post\u ID=43。1064-您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解要使用的正确语法,从选择wp_posts.ID,wp_posts.post_modified_gmt,第4行的'wp',在'on子句'中设置错误1054-未知列'wp_posts.ID'。我还删除了左连接之前的逗号,因为它抛出了错误。有什么想法吗?出了什么问题?是的,正在修正答案。左连接需要在wp_posts之后。由于连接位于wp_posts.ID=,在尝试使用term_关系离开连接wp_posttea之前出现错误,并且wp_posttea中没有ID。我现在已经修复了。我已经编辑了我的帖子来显示我得到的输出,难道没有办法将meta_键值转换为列名吗?谢谢你看一眼。。。虽然你们可能需要很多案例…但通过添加到“展平”然后结果,它可以在需要的组中工作。谢谢你的帮助,你是个英雄;正在检查转置..在找到了一些内容:但看起来很混乱…由于mysql不支持本机转置/旋转,因此解决方案比您想要的更复杂。BBL:我更新了我的答案,用案例做了透视。试试看,告诉我: