MySQL中insert into语句的6种写法

MySQL中insert into语句的6种写法,第1张

insert into是mysql中最常用的插入语句,它有6种写法。

如果插入的记录是数字的话要在数字的逗号后面加n:

通过以上实例我们可以看到insert into语句只能向原表中插入于其字段对应的数据,那么能不能通过insert into语句来把其他表的数据插入到原表中呢:

在MySQL中set方法

ModifyStatement.Set Method修改语句 set方法

Sets key and value. 设置键和值。

由于insert into语句是一个插入性的语句,所以它的功能要么向指定的表插入数据

也许你看到这个SQL语句是正确的,就觉得这样应该也可以:

mysql>mysql>insert into 4a set sname=4ainall.sname

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql>insert into 4a set sname=4ainall.sname' at line 1

或者这样也可以:

mysql>mysql>insert into 4a set sname="赵六"

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'mysql>insert into 4a set sname="赵六"' at line 1

然后这样也是不可用:

mysql>insert into 4a select * from 4ainall set sname=4ainall.sname

ERROR 1064 (42000): You have an error in your SQL syntaxcheck the manual that corresponds to your MySQL server version for the right syntax to use near 'from 4ainall set sname=4ainall.sname' at line 1

可以看出由于select是作用于4inall这个表的,而set方法也只能在select语句中,这就直接导致set方法只能作用于4inall这个表,而无法作用于4a这个表。

但是如果我们不用select语句的话编译器又怎么会知道4inall表中的数据在哪里?

显然select是用于查的而set则是一个用于改的方法,两者无法结合在一起——insert into set语句当然也不能用于将其他表的数据插入到原表中了。

update a set flag='p' where flag is null

你这图片不是插入页吗?

如果你确定真没有值写入,首先查一下,你的set列中是否有'p'还是'P',更新完后可以用show warnings查看警告信息

补充:

很有可能你指的空值不是null,而是'',所以应该

update a set flag='p' where flag=''

尝试一下。

1、首先打开MYSQL的管理工具,新建一个test表,并且在表中插入两个字段。

2、接下来在Editplus编辑器中创建一个PHP文件,进行数据库连接,并且选择要 *** 作的数据库。

3、然后通过mysql_query方法执行一个Insert的插入语句。

4、执行完毕以后,回到数据库管理工具中,这个时候你会发现插入的中文乱码了。

5、接下来在PHP文件中通过mysql_query执行一个set  names  utf8语句。

6、接下来执行以后回到MYSQL数据库中,发现插入的中文显示正常了,即成功往mysql中写入数据了。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/8578333.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-18
下一篇 2023-04-18

发表评论

登录后才能评论

评论列表(0条)

保存