sql在查询之后插入到表中

im9ewurl  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(339)

所以我不知道该怎么做。我有一个如下所示的表[enter image description here][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())

在我的select查询之前,这不是它应该如何工作的。
任何指导都是惊人的!!
提前谢谢大家

pu82cl6c

pu82cl6c1#

在你的 INSERT INTO 语句将要插入的字段名放入。您没有名为 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

相关问题