MySql常用SQL增删改查(精)

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

SQL语句的添加、删除、修改

添加、删除、修改使用db.Execute(Sql命令执行操作

(一 Select语句

A. 普通查询

1. 查询全部的行和列

select * from users

2. 查询部分的行并用as命名列(as可用空格代替)

select UserName as 用户名,NickName 密码,sex from users where sex = 0

select UserName,NickName,sex from users where sex <> 0

3. 查询空行

i. select UserName from users where sex is null;

ii. select UserName from users where sex = "";

4. 使用常量列

select UserName as 用户名,"中北大学" as "学校" from users;

5. 限制固定行数

select UserId ,UserName as 用户名 from users limit 2,4;

6. 升序排列降序排列按照多列排序

i. select * from users where Height >178 order by age asc;

ii. select * from users where Height >178 order by age desc;

iii. select UserName as 用户名,Height 身高,Age as 年龄 from users where Age>20 order by Height asc, Age desc;

7. 随机查询前20条数据

Select * from users order by rand ( limit 20;

8. 关键字顺序:

Where------group by-----having-----order by

B. 高级查询:

1. 模糊查询——like

select SName as 姓名from Students where Sname like ‘张%’;

2. 模糊查询——IS NULL

select Sname as 姓名,SAddress as 地址 form Students where SAddress is null

3. 模糊查询——between

select StudentId,Score from Score where Score between 60 and 80

4. 模糊查询——in

select Sname as 学员姓名,SAddress as 地址from Students where SAddress in(‘北京’,’广州’,’上海’;

C. 聚合函数

D. 分组查询

a 分组查询——GROUP BY

b 分组查询——多列分组

c 分组查询——HAVING

d 分组查询总结

i. where字句从数据源中去掉不符合其搜索条件的数据

ii. group by 字句搜集数据航到各个组中

iii. 统计函数为各个组计算统计值

iv. having 字句去掉不符合其搜索条件的各组数据行

v. 使用group by 时,select后面出现的内容要么为聚合函数,要么为group by 后面出现的内容vi. 关键字的先后顺序:where——group by——having——order by

E. 多表连接查询

分类:

内连接(inner join):(等值连接、内连接)只返回两个表中连结字段相等的行

外连接:

1 左外连接(left join)返回包括左表中的所有记录和右表中连结字段相等的记录

2 右连接(right join):返回包括右表中的所有记录和左表中连结字段相等的记

录。

F. 子查询

在某些特定的业务需求下,当进行查询的时候,需要的条件是另一个select语句的结果,这个时候,就要用到子查询。用于子查询的关键字包括:in、not in、

=、!=、exists、not exists…

1. 子查询在where语句中的一般用法:

2. 子查询——in

3. 子查询——not in

4. 子查询——exists

5. 子查询——not exists

6. 子查询——from中子查询

7. 子查询——any、in、some

any意思任意一个,即大于任意一个即可,注意与all的区别

8. 子查询——ALL

all意思所有的值,注意与any的区别

not in 是ALL的别名

9. 补充:

Sql = "Select Distinct 字段名 From 数据表"

Distinct函数,查询数据库存表内不重复的记录

Sql = "Select Count(* From 数据表 where 字段名1>#18:0:0#

and 字段名1< #19:00# "

count函数,查询数库表内有多少条记录,“字段名1”是指同一字段

例:

set rs=conn.execute("select count(id as idnum from news"

response.write rs("idnum"

sql="select * from 数据表 where 字段名 between 值1 and 值2"

Sql="select * from 数据表 where 字段名 between #2003-8-10#

and #2003-8-12#"

在日期类数值为2003-8-10 19:55:08 的字段里查找2003-8-10至

2003-8-12的所有记录,而不管是几点几分。

select * from tb_name where datetime between #2003-8-10# and

#2003-8-12#

字段里面的数据格式为:2003-8-10 19:55:08,通过sql查出2003-

8-10至2003-8-12的所有纪录,而不管是几点几分。

Sql="select * from 数据表 where 字段名=字段值 order by 字段

名 [desc]"

Sql="select * from 数据表 where 字段名 like '%字段值%' order

by 字段名 [desc]"

模糊查询

Sql="select top 10 * from 数据表 where 字段名 order by 字段

名 [desc]" 查找数据库中前10记录

Sql="select top n * form 数据表 order by newid("

随机取出数据库中的若干条记录的方法

top n,n就是要取出的记录数

Sql="select * from 数据表 where 字段名 in ('值1','值2','值3'"

相关文档
最新文档