数据库中create table temp ( city char(16) not null, qtysum float ) insert into temp(city, qtysum) select s.city, SUM(qty) from s, spj where s.sno = spj.sno and (s.city = '上海' or s.city = '天津') group by s.city出现[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into temp(city, qtysum)' at line 6怎么办
时间: 2023-12-17 13:05:55 浏览: 43
这个错误是因为在使用多条 SQL 语句时没有正确分隔它们。你需要在两条 SQL 语句之间添加分号 (;)。
正确的 SQL 语句应该是这样的:
```
create table temp (
city char(16) not null,
qtysum float
);
insert into temp(city, qtysum)
select s.city, SUM(qty)
from s, spj
where s.sno = spj.sno and (s.city = '上海' or s.city = '天津')
group by s.city;
```
注意,在 `create table` 和 `insert into` 语句之间添加了分号。这样就可以正确执行 SQL 语句了。
相关问题
Insert into select和create table as哪个效率更好
Insert into select 和 create table as 都是将一个表中的数据插入到另一个表中,但它们的实现方式不同,因此它们的效率也有所差异。
Insert into select 语句是将数据从一个表中选择出来,然后插入到另一个表中。这种方式的好处是可以在插入时进行筛选和转换,但是其缺点是需要先查询数据,再将查询结果进行插入,因此对于数据量比较大的情况下,效率会比较低。
Create table as 语句是将一个表的结构和数据复制到新表中。这种方式的好处是可以直接复制表结构和数据,因此比 Insert into select 语句更快。但是其缺点是不能进行筛选和转换,如果需要对数据进行处理,则需要使用其他语句。
总的来说,在处理大量数据时,create table as 的效率会比 insert into select 更高,但是在需要对数据进行处理时,insert into select 更加灵活。
create table as和insert into
1. create table as:创建表并将另一个表的数据复制到新表中。语法如下:
```
CREATE TABLE new_table AS
SELECT column1, column2, ...
FROM existing_table
WHERE condition;
```
例如:
```
CREATE TABLE new_table AS
SELECT name, age, gender
FROM student
WHERE grade = 'A';
```
这将创建一个名为new_table的新表,并将student表中成绩为A的学生的姓名、年龄和性别复制到新表中。
2. insert into:将数据插入到现有表中。语法如下:
```
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
```
例如:
```
INSERT INTO student (name, age, gender, grade)
VALUES ('Tom', 18, 'M', 'A');
```
这将在student表中插入一条新记录,包括姓名为Tom、年龄为18、性别为M、成绩为A的信息。