は、私は以下のデータが発生している。そして、CONCAT()GROUP_CONCAT()内
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 | 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 |
---------------------------------------------------------------------
この貫通
--------------------------------------------------------------
| 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
私の問題は、どのように私は1つのカラムになるために自分のデータを得ることができ、次のとおりです。
--------------------------------------------------------------
| id | date | order_items |
--------------------------------------------------------------
| | | 2 Alfonso XO |
| 1460 | 2017-03-24 | 3 Godiva Dark 85% Cacao |
| | | 5 Godiva Gold Rigid Ballotin 14pcs |
--------------------------------------------------------------
私はgroup_concat()
と私のconcat()
を囲んでみましたが、私はエラーが表示さ:Invalid group function
を。正しい答えのためのサーGiorgos Betsosへ
UPDATE
ビッグ小道具。
これが今の私のクエリです:
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
はしかし、もう少し私のクエリを微調整、I別の問題時:
1.私はから新しく生成された1上の別のデータを連結してみました新しいクエリ:
-----------------------------------------------------------------------
| 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
BUTとp.post_type = 'product'
で接続されています。
concat()
をconcat_ws()
に置き換えようとしましたが、運がありませんでした。
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
結果セットは表形式ではありません。あなたは1行または3行をしたいですか? –
MAXを一度も試してみると、問題が発生しているかどうかを確認することができます。グループ機能が使用されているため、そのエラーが発生していると思います。 –
@GordonLinoff申し訳ありませんが、1行だけです。 –