300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 蓬莱小课:数据分析种SQL语句中单引号 双引号的用法

蓬莱小课:数据分析种SQL语句中单引号 双引号的用法

时间:2022-08-06 11:52:07

相关推荐

蓬莱小课:数据分析种SQL语句中单引号 双引号的用法

今天这篇文章主要给大家介绍一下SQL语句中单引号、双引号的使用方法。虽然说的是Insert语句, 但是Select、Update、Delete语句都是一样的,具有一定的参考价值,需要的小伙伴可以参考一下!

(左右滑动查看完整代码)

1、假如有下述表格

表名:usertable

2、插入字符串型

假如要插入一个名为小小的人,因为是字符串,所以Insert语句中名字两边要加单引号,数值型可以不加单引号如:

Insert into usertable(username) values('小小')

如果现在姓名是一个变量thename,则写成:

Insert into usertable(username) values('" & thename & "')

ps:&改为+号也可以,字符串连接

3、插入数字型

假如插入一个年龄为12的记录,要注意数字不用加单引号:

Insert into usertable(age) values(12)

如果现在年龄是一个变量theage,则为:

Insert into usertable(age) values(“ & theage & “)

4、插入日期型

日期型和字符串型类似,也是用单引号:

Insert into usertable(birthday) values('1998-10-01')

如果换成日期变量thedate:

Insert into usertable(birthday) values('” & thedate & “')

5、插入布尔型

布尔型和数字型类似,只不过只有两个值 True和False,比如:

Insert into usertable(marry) values(True)

如果换成布尔变量themarry:

Insert into usertable(birthday) values(” & themarry& “)

6、综合示例

插入一个姓名为小小,年龄为12的记录:

Insert into usertable(username,age) values('小小',12)

仔细注意上式:因为姓名是字符串,所以小小两边加了单引号;年龄是数字,所以没有加单引号。

如果换成字符串变量thename和数字变量theage,则变为:

Insert into usertable(username,age) values('” & thename & “','” & theage & ')

7、小窍门

要把下面的语句题换成变量的写法:

Insert into usertable(username) values('小小')

第一步:先把小小抹去,在原位置加两个引号

Insert into usertable(username) values('” “')

第二步:在中间添加两个连接符&

Insert into usertable(username) values('” & & “')

第三步:把变量写在两个连接符之间

Insert into usertable(username) values('” & thename & “')

我们在写SQL查询的时候建议还是不厌其烦的加上单引号,除了麻烦些并没有坏处。因为对于主键为字符串类型的查询语句,加不加单引号的性能是相差比较大的~

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。