当前位置: 移动技术网 > IT编程>数据库>MSSQL > 整理MySql常用查询语句(23种)

整理MySql常用查询语句(23种)

2017年12月12日  | 移动技术网IT编程  | 我要评论

铜板价格,梦见扫落叶,贝因美网站

废话不多了,直接贴代码了

一查询数值型数据:

select * from tb_name where sum > 100;

 查询谓词:>,=,<,<>,!=,!>,!<,=>,=<

二查询字符串

 select * from tb_stu where sname = '小刘'
 select * from tb_stu where sname like '刘%'
 select * from tb_stu where sname like '%程序员'
 select * from tb_stu where sname like '%php%'

三查询日期型数据

select * from tb_stu where date = '2011-04-08'

 注:不同数据库对日期型数据存在差异:

 (1)mysql:select * from tb_name where birthday = '2011-04-08'

(2)sql server:select * from tb_name where birthday = '2011-04-08'


 (3)access:select * from tb_name where birthday = #2011-04-08#

四查询逻辑型数据

 select * from tb_name where type = 't'
 select * from tb_name where type = 'f'

 逻辑运算符:and or not

五查询非空数据

 select * from tb_name where address <>'' order by addtime desc

 注:<>相当于php中的!=

六利用变量查询数值型数据

select * from tb_name where id = '$_post[text]' 

注:利用变量查询数据时,传入sql的变量不必用引号括起来,因为php中的字符串与数值型数据进行连接时,程序会自动将数值型数据转变成字符串,然后与要连接的字符串进行连接

七利用变量查询字符串数据

select * from tb_name where name like '%$_post[name]%' 

完全匹配的方法"%%"表示可以出现在任何位置

八查询前n条记录

select * from tb_name limit 0,$n;

 limit语句与其他语句,如order by等语句联合使用,会使用sql语句千变万化,使程序非常灵活

九查询后n条记录

select * from tb_stu order by id asc limit $n

十查询从指定位置开始的n条记录

 select * from tb_stu order by id asc limit $_post[begin],$n

 注意:数据的id是从0开始的

十一查询统计结果中的前n条记录

select * ,(yw+sx+wy) as total from tb_score order by (yw+sx+wy) desc limit 0,$num 

十二查询指定时间段的数据

 select  要查找的字段 from 表名 where 字段名 between 初始值 and 终止值

select * from tb_stu where age between 0 and 18


十三按月查询统计数据

select * from tb_stu where month(date) = '$_post[date]' order by date ;

 注:sql语言中提供了如下函数,利用这些函数可以很方便地实现按年、月、日进行查询

 year(data):返回data表达式中的公元年分所对应的数值

 month(data):返回data表达式中的月分所对应的数值

 day(data):返回data表达式中的日期所对应的数值

十四查询大于指定条件的记录

select * from tb_stu where age>$_post[age] order by age;

十五查询结果不显示重复记录

 select distinct 字段名 from 表名 where 查询条件

注:sql语句中的distinct必须与where子句联合使用,否则输出的信息不会有变化 ,且字段不能用*代替 

十六not与谓词进行组合条件的查询

 (1)not berween … and … 对介于起始值和终止值间的数据时行查询 可改成 <起始值 and >终止值

 (2)is not null 对非空值进行查询

 (3)is null 对空值进行查询

 (4)not in 该式根据使用的关键字是包含在列表内还是排除在列表外,指定表达式的搜索,搜索表达式可以是常量或列名,而列名可以是一组常量,但更多情况下是子查询

十七显示数据表中重复的记录和记录条数

 select name,age,count(*) ,age from tb_stu where age = '19' group by date

十八对数据进行降序/升序查询

 select 字段名 from tb_stu where 条件 order by 字段 desc 降序

 select 字段名 from tb_stu where 条件 order by 字段 asc  升序

 注:对字段进行排序时若不指定排序方式,则默认为asc升序

十九对数据进行多条件查询

 select 字段名 from tb_stu where 条件 order by 字段1 asc 字段2 desc  …

 注意:对查询信息进行多条件排序是为了共同限制记录的输出,一般情况下,由于不是单一条件限制,所以在输出效果上有一些差别。

二十对统计结果进行排序

 函数sum([all]字段名) 或 sum([distinct]字段名),可实现对字段的求和,函数中为all时为所有该字段所有记录求和,若为distinct则为该字段所有不重复记录的字段求和

 如:

select name,sum(price) as sumprice from tb_price group by name 


select * from tb_name order by mount desc,price asc 

二十一单列数据分组统计

 select id,name,sum(price) as title,date from tb_price group by pid order by title desc

 注:当分组语句group by排序语句order by同时出现在sql语句中时,要将分组语句书写在排序语句的前面,否则会出现错误

二十二多列数据分组统计

 多列数据分组统计与单列数据分组统计类似

select *,sum(字段1*字段2) as (新字段1) from 表名 group by 字段 order by 新字段1 desc

 select id,name,sum(price*num) as sumprice from tb_price group by pid order by sumprice desc

 注:group by语句后面一般为不是聚合函数的数列,即不是要分组的列

二十三多表分组统计

 select a.name,avg(a.price),b.name,avg(b.price) from tb_demo058 as a,tb_demo058_1 as b where a.id=b.id group by b.type;

就是内容就是本人给大家整理的mysql常用查询语句(23种),希望能帮助到大家。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网