簡體   English   中英

SQL在查詢后插入表中

[英]SQL insert into a table after a query

所以我不確定如何處理這個問題。 我有一個看起來像這樣的表[在這里輸入圖像描述] [1]

![1]: https//i.stack.imgur.com/k9Cl7.png

我需要從不同的表中獲取信息並將其插入上表。

獲取我需要的信息的查詢如下:

select customerNumber, sum(quantityOrdered * priceEach) as orderTotal, curdate() as todaysDate 
from orders join orderdetails

where orders.orderNumber = orderdetails.orderNumber

group by orders.customerNumber

having orderTotal > 150000

我嘗試過使用:

insert into topCustomers( customerNumber, orderTotal, curdate())

在我的選擇查詢之前,這不是它應該如何工作。

任何指導都會很棒!!

先謝謝大家

INSERT INTO語句的括號中,放置要插入的FIELD NAMES。 您沒有名為CURDATE()的字段名稱,因此查詢失敗。 代替:

insert into topCustomers( customerNumber, orderTotal, CustomerDate) 
select customerNumber, sum(quantityOrdered * priceEach) as orderTotal, curdate() as todaysDate 
from orders join orderdetails    
where orders.orderNumber = orderdetails.orderNumber    
group by orders.customerNumber    
having orderTotal > 150000

例:

mysql> create table test2 (cdate date);
Query OK, 0 rows affected (0.32 sec)

mysql> INSERT INTO test2 (curdate());
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual tcorresponds to your MySQL server version for the right syntax to use near 'cte())' at line 1

mysql> INSERT INTO test2 (cdate) SELECT curdate();
Query OK, 1 row affected (0.07 sec)
Records: 1  Duplicates: 0  Warnings: 0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM