无法使用此SQL:
INSERT INTO final_tab (
id_member_card
,points
)
VALUES
(
SELECT idMemberCard
,punti FROM (
SELECT *
FROM (
SELECT idMemberCard
,SUM(amountOriginal) AS speso
,SUM(amountFinal) AS punti
FROM transactions
WHERE date_transaction_UTC BETWEEN '2018-01-01 00:00:00'
AND '2018-03-19 23:59:59'
GROUP BY idMemberCard
) AS derived
WHERE speso >= 50
) AS d2
)
错误是
语法错误附近' SELECT idMemberCard,punti FROM(SELECT * FROM(SELECT idMemberCard,SUM(amou' at the line 6)
我哪里错了?谢谢
答案 0 :(得分:4)
VALUES
不需要INSERT . . . SELECT
:
INSERT INTO final_tab (id_member_card , points)
SELECT idMemberCard , punti
FROM (SELECT *
FROM (SELECT idMemberCard , SUM(amountOriginal) AS speso,
SUM(amountFinal) AS punti
FROM transactions
WHERE date_transaction_UTC BETWEEN '2018-01-01 00:00:00' AND '2018-03-19 23:59:59'
GROUP BY idMemberCard
) AS derived
WHERE speso >= 50
) t2;
您可以大大简化逻辑:
INSERT INTO final_tab (id_member_card , points)
SELECT idMemberCard , SUM(amountOriginal) AS speso, SUM(amountFinal) AS punti
FROM transactions
WHERE date_transaction_UTC >= '2018-01-01' AND
date_transaction_UTC < '2018-03-20'
GROUP BY idMemberCard
HAVING speso >= 50;
如果您不想将值实际存储在表中,也可以将其指定为视图。
答案 1 :(得分:1)
您可以直接将其表达为:
INSERT INTO final_tab (id_member_card , points)
SELECT idMemberCard , punti
FROM (SELECT * FROM
( SELECT idMemberCard , SUM(amountOriginal) AS speso,
SUM(amountFinal) AS punti
FROM transactions
WHERE date_transaction_UTC BETWEEN '2018-01-01 00:00:00' AND '2018-03-19 23:59:59'
GROUP BY idMemberCard ) AS derived
WHERE speso >= 50
但是,您的核心逻辑将重写为:
INSERT INTO final_tab (id_member_card , points)
SELECT idMemberCard , SUM(amountFinal) AS punti,
SUM(amountOriginal) AS speso,
FROM transactions
WHERE date_transaction_UTC BETWEEN '2018-01-01 00:00:00' AND '2018-03-19 23:59:59'
GROUP BY idMemberCard
HAVING speso > 50;