我有3张桌子:
表1:表名称:FULL_DATA
DATE ORDER_NO AMOUNT MODE
01-04-2019 ADVANVCE.:ORD1971718 5000 DEBIT-CARD
01-04-2019 ADVANVCE.:ORD1961718 6000 CASH
01-04-2019 ADVANVCE.:ORD2001819 8000 CASH
02-04-2019 ADVANVCE.:ORD2011819 10000 CASH
02-04-2019 ADVANVCE.:ORD0891819 3000 CASH
02-04-2019 ADVANVCE.:ORD2021819 8000 DEBIT-CARD
02-04-2019 ADVANVCE.:ORD2031819 12000 CASH
03-04-2019 ADVANVCE.:ORD2041819 14000 CASH
03-04-2019 ADVANVCE.:ORD2051819 17000 CASH
03-04-2019 ADVANVCE.:ORD2061819 14000 CASH
03-04-2019 ADVANVCE.:ORD2061819 15000 DEBIT-CARD
03-04-2019 ADVANVCE.:ORD2071819 4000 DEBIT-CARD
03-04-2019 ADVANVCE.:ORD2071819 1000 CASH
04-04-2019 ADVANVCE.:ORD2101819 500 CASH
04-04-2019 ADVANVCE.:ORD2091819 1000 CASH
04-04-2019 ADVANVCE.:ORD2081819 2000 CASH
04-04-2019 ADVANVCE.:ORD0471819 8000 CASH
表2:表名称:ORDER_DETAILS
ORDER_NO ITEM_NAME CUSTOMER_ID
ORD2001819 ITEM-1 7
ORD2011819 ITEM-2 6
ORD2021819 ITEM-3 5
ORD2021819 ITEM-4 5
ORD2021819 ITEM-5 5
ORD2021819 ITEM-6 5
ORD2031819 ITEM-7 8
ORD2041819 ITEM-8 67
ORD2041819 ITEM-9 67
ORD2051819 ITEM-10 89
ORD2061819 ITEM-11 54
ORD2061819 ITEM-12 54
ORD2071819 ITEM-13 11
ORD2101819 ITEM-14 12
ORD2091819 ITEM-15 14
ORD2081819 ITEM-16 16
ORD1971718 ITEM-17 68
ORD1971718 ITEM-18 68
ORD0471819 ITEM-19 90
ORD0891819 ITEM-20 44
ORD1961718 ITEM-21 55
表3:表名称:CUSTOMER_DETAILS
CUSTOMER_ID CUSTOMER_NAME
7 CUSTOMER-1
6 CUSTOMER-24
5 CUSTOMER-39
8 CUSTOMER-40
67 CUSTOMER-15
89 CUSTOMER-66
54 CUSTOMER-7
11 CUSTOMER-89
12 CUSTOMER-9
14 CUSTOMER-10
16 CUSTOMER-11
68 CUSTOMER-121
90 CUSTOMER-134
44 CUSTOMER-147
55 CUSTOMER-158
查询JOIN
的这些表,它们将从FULL_DATA表中获取日期,ORDER_NO,数量,模式列以及JOIN
的ORDER_NO(通过使用ORDER_DETAILS表将订单号分割成“:”来获取订单号)并获取它的客户ID,然后JOIN
及其客户ID与客户详细信息表并获取客户名称????
看起来像这样的数据:
最终查询结果:
DATE CUSTOMER_NAME ORDER_NO AMOUNT MODE
01-04-2019 CUSTOMER-121 ADVANVCE.:ORD1971718 5000 DEBIT-CARD
01-04-2019 CUSTOMER-158 ADVANVCE.:ORD1961718 6000 CASH
01-04-2019 CUSTOMER-1 ADVANVCE.:ORD2001819 8000 CASH
02-04-2019 CUSTOMER-24 ADVANVCE.:ORD2011819 10000 CASH
02-04-2019 CUSTOMER-147 ADVANVCE.:ORD0891819 3000 CASH
02-04-2019 CUSTOMER-39 ADVANVCE.:ORD2021819 8000 DEBIT-CARD
02-04-2019 CUSTOMER-40 ADVANVCE.:ORD2031819 12000 CASH
03-04-2019 CUSTOMER-15 ADVANVCE.:ORD2041819 14000 CASH
03-04-2019 CUSTOMER-66 ADVANVCE.:ORD2051819 17000 CASH
03-04-2019 CUSTOMER-7 ADVANVCE.:ORD2061819 14000 CASH
03-04-2019 CUSTOMER-7 ADVANVCE.:ORD2061819 15000 DEBIT-CARD
03-04-2019 CUSTOMER-89 ADVANVCE.:ORD2071819 4000 DEBIT-CARD
03-04-2019 CUSTOMER-89 ADVANVCE.:ORD2071819 1000 CASH
04-04-2019 CUSTOMER-9 ADVANVCE.:ORD2101819 500 CASH
04-04-2019 CUSTOMER-10 ADVANVCE.:ORD2091819 1000 CASH
04-04-2019 CUSTOMER-11 ADVANVCE.:ORD2081819 2000 CASH
04-04-2019 CUSTOMER-134 ADVANVCE.:ORD0471819 8000 CASH
答案 0 :(得分:1)
如果数据中的订单号始终是:
之后的最后一部分?
SELECT
d.DATE,
GROUP_CONCAT(DISTINCT c.CUSTOMER_NAME) AS CUSTOMER_NAMES,
d.ORDER_NO,
d.AMOUNT,
d.MODE
FROM FULL_DATA d
LEFT JOIN ORDER_DETAILS o
ON o.ORDER_NO = RTRIM(SUBSTRING_INDEX(d.ORDER_NO,':',-1))
LEFT JOIN CUSTOMER_DETAILS c ON c.CUSTOMER_ID = o.CUSTOMER_ID
GROUP BY
d.DATE,
d.ORDER_NO,
d.AMOUNT,
d.MODE
每个订单有多个商品。
从理论上讲,每个订单可能有1个以上的客户。
因此,此处使用GROUP_CONCAT而不是MAX。
答案 1 :(得分:0)
它将从full_data表中获取每个订单号,并使用第二和第三表查找没有订单的客户详细信息,并在日期列后显示我的客户名称
答案 2 :(得分:0)
我通过从order_detaisl为customer_id和order_no选择不同的值进行了加入
SELECT f.DATE, CUSTOMER_NAME, f.ORDER_NO, AMOUNT, MODE
FROM FULL_DATA f
JOIN (SELECT DISTINCT customer_id, order_no
FROM ORDER_DETAILS) o ON SUBSTRING_INDEX(f.ORDER_NO,':',-1)= o.ORDER_NO
JOIN CUSTOMER_DETAILS c ON o.CUSTOMER_ID = c.CUSTOMER_ID
WHERE f.DATE BETWEEN '2019-04-01' AND '2019-04-04'
ORDER BY f.DATE ASC