1
我有以下數據結果:CONCAT()的內部GROUP_CONCAT()
---------------------------------------------------------------------
| id | date | order_items | qty |
---------------------------------------------------------------------
| 1460 | 2017-03-24 | Alfonso XO | 2 |
| 1460 | 2017-03-24 | Godiva Dark 85% Cacao | 3 |
| 1460 | 2017-03-24 | Godiva Gold Rigid Ballotin 14pcs | 5 |
---------------------------------------------------------------------
通過此查詢:
SELECT
p.ID,
date(p.post_date) AS date,
i.order_item_name AS order_items
max(CASE WHEN o.meta_key = '_qty' AND i.order_item_id = o.order_item_id THEN o.meta_value END) AS qty
FROM wp_posts AS p
LEFT JOIN wp_woocommerce_order_items AS i ON p.ID = i.order_id
LEFT JOIN wp_woocommerce_order_itemmeta AS o ON i.order_item_id = o.order_item_id
WHERE p.ID = 1460 AND i.order_item_type = 'line_item'
GROUP BY i.order_item_name
然後此:
--------------------------------------------------------------
| id | date | order_items |
--------------------------------------------------------------
| 1460 | 2017-03-24 | 2 Alfonso XO |
| 1460 | 2017-03-24 | 3 Godiva Dark 85% Cacao |
| 1460 | 2017-03-24 | 5 Godiva Gold Rigid Ballotin 14pcs |
--------------------------------------------------------------
通過這樣的:
SELECT
p.ID,
date(p.post_date) AS date,
concat(max(CASE WHEN imeta.meta_key = '_qty' and items.order_item_id = imeta.order_item_id THEN imeta.meta_value END), ' ', items.order_item_name) as order_items
FROM wp_posts AS p
LEFT JOIN wp_woocommerce_order_items AS i ON p.ID = i.order_id
LEFT JOIN wp_woocommerce_order_itemmeta AS o ON i.order_item_id = o.order_item_id
WHERE p.ID = 1460 AND i.order_item_type = 'line_item'
GROUP BY i.order_item_name
我的問題是,我如何能實現我的數據變成一列:
--------------------------------------------------------------
| id | date | order_items |
--------------------------------------------------------------
| | | 2 Alfonso XO |
| 1460 | 2017-03-24 | 3 Godiva Dark 85% Cacao |
| | | 5 Godiva Gold Rigid Ballotin 14pcs |
--------------------------------------------------------------
我試圖包圍我concat()
與group_concat()
但是我收到一個錯誤:Invalid group function
。
UPDATE
大道具爵士Giorgos Betsos爲正確答案。
現在這是我的查詢:
SELECT ID, post_date, GROUP_CONCAT(order_items separator '\n') AS order_items, post_status, shipping_flight_number, letter_code, terminal, order_total, full_name
FROM (
SELECT
p.ID,
date(p.post_date) AS post_date,
p.post_status AS post_status,
concat(max(CASE
WHEN o.meta_key = '_qty' and
i.order_item_id = o.order_item_id
THEN o.meta_value
END), ' ', i.order_item_name) as order_items,
max(CASE WHEN pm.meta_key = 'shipping_flight_number' and p.ID = pm.post_id THEN pm.meta_value END) as shipping_flight_number,
substring_index(max(CASE WHEN pm.meta_key = 'shipping_flight_number' and p.ID = pm.post_id THEN pm.meta_value END)," ",1) as letter_code,
(select terminal_id from wp_shipping_airlines where letter_code = substring_index(max(CASE WHEN pm.meta_key = 'shipping_flight_number' and p.ID = pm.post_id THEN pm.meta_value END)," ",1)) as terminal,
max(CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END) as order_total,
concat(max(CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END), ' ', max(CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END)) as full_name
FROM wp_posts AS p
LEFT JOIN wp_postmeta pm on p.ID = pm.post_id
LEFT JOIN wp_woocommerce_order_items AS i ON p.ID = i.order_id
LEFT JOIN wp_woocommerce_order_itemmeta AS o ON i.order_item_id = o.order_item_id
WHERE p.post_type = 'shop_order' AND i.order_item_type = 'line_item' AND p.post_status != 'trash'
GROUP BY i.order_item_name, p.ID) AS t
GROUP BY ID, post_date, shipping_flight_number, letter_code, terminal, order_total, full_name
然而,在調整我的查詢多一點,我的另一個問題:
我試圖拼接上新生成一個從另一個數據新查詢:
-----------------------------------------------------------------------
| id | date | order_items |
-----------------------------------------------------------------------
| | | 2 Alfonso XO (2300551) |
| 1460 | 2017-03-24 | 3 Godiva Dark 85% Cacao (2657010) |
| | | 5 Godiva Gold Rigid Ballotin 14pcs (2421181) |
-----------------------------------------------------------------------
在括號中的那些被稱爲SKU的,他們可以在wp_postmeta表中查到,但是他們是通過p.ID
和p.post_id
連接,但是與p.post_type = 'product'
連接。
我試着用concat_ws()
代替concat()
,但沒有運氣。
SELECT ID, post_date, GROUP_CONCAT(order_items separator '\n') AS order_items, post_status, shipping_flight_number, letter_code, terminal, order_total, full_name
FROM (
SELECT
p.ID,
date(p.post_date) AS post_date,
p.post_status AS post_status,
concat_ws(' ', max(CASE
WHEN o.meta_key = '_qty' and
i.order_item_id = o.order_item_id
THEN o.meta_value
END), i.order_item_name, ' -- ', max(CASE WHEN pm.meta_key = '_sku' and p.ID = pm.post_id and p.post_type = 'product' THEN pm.meta_value END)) as order_items,
max(CASE WHEN pm.meta_key = 'shipping_flight_number' and p.ID = pm.post_id THEN pm.meta_value END) as shipping_flight_number,
substring_index(max(CASE WHEN pm.meta_key = 'shipping_flight_number' and p.ID = pm.post_id THEN pm.meta_value END)," ",1) as letter_code,
(select terminal_id from wp_shipping_airlines where letter_code = substring_index(max(CASE WHEN pm.meta_key = 'shipping_flight_number' and p.ID = pm.post_id THEN pm.meta_value END)," ",1)) as terminal,
max(CASE WHEN pm.meta_key = '_order_total' and p.ID = pm.post_id THEN pm.meta_value END) as order_total,
concat(max(CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END), ' ', max(CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END)) as full_name
FROM wp_posts AS p
LEFT JOIN wp_postmeta pm on p.ID = pm.post_id
LEFT JOIN wp_woocommerce_order_items AS i ON p.ID = i.order_id
LEFT JOIN wp_woocommerce_order_itemmeta AS o ON i.order_item_id = o.order_item_id
WHERE p.post_type = 'shop_order' AND i.order_item_type = 'line_item' AND p.post_status != 'trash'
GROUP BY i.order_item_name, p.ID) AS t
GROUP BY ID, post_date, shipping_flight_number, letter_code, terminal, order_total, full_name
您的幫助是非常感謝。
非常感謝。
-Eli
您的結果集不是表格格式。你想要一行還是三行? –
嘗試不使用MAX一次檢查是否導致問題,我猜是因爲使用了Group函數而導致拋出錯誤。 –
@GordonLinoff抱歉,只有1排。 –