您的位置:首页 > 其它

单表数据记录查询

2016-06-24 09:13 169 查看
一、DISTINCT的用法(查询多个字段,仅对其中一个去重复)

distinct必须放在开头

错: select id, distinct name from table


正确写法



select *, COUNT(DISTINCT Sno )  from student_course GROUP BY Sno




二、实现数学四则运算数据库查询(+-*/%)

select ename,moonSalay*12 as yearSalary from t_employee;


三、函数CONCAT()来连接字符串

//Mary的年薪为:800000.00
select concat(ename,‘雇员的年薪为:’,moonSalary*12 ) yearSalary from employee;


四、BETWEEN AND(NOT BETWEEN AND)

//包含边界
select * FROM student_course where  student_course.grade  BETWEEN 50 and  80


五。IS NULL(IS NOT NULL)

带IS NULL 关键字的空值查询

select * from student where grade IS not null


六、IN ( NOT IN )

select * from student where stuNum In (10,21,56,54,89)


注:由于NULL不能进行如何的“操作”

–如果null参与算术运算,则该算术表达式的值为null。(例如:+,-,*,/ 加减乘除)

–如果null参与比较运算,则结果可视为false。(例如:>=,<=,<> 大于,小于,不等于)

–如果null参与聚集运算,则聚集函数都置为null。除count(*)之外。

–如果在not in子查询中有null值的时候,则不会返回数据。

--正确写法
SELECT  * FROM   dbo.Table_A AS a
WHERE   a.ID NOT IN ( SELECT    b.ID
FROM      dbo.Table_B AS b
WHERE     b.ID IS NOT NULL )
--排除NULL值参与运算符比较


七、Like

“_”通配符:能匹配单个字符

“%”通配符:能匹配任意长度,既可以是0个又可以是一个,还可以是N个

select * from student where name Like 'A%'
select * from student where name Like 'a%'
//查询结果一样,不区分大小写
select * from student where NOT name Like 'A%'
select * from student where name Like '_A%'
//查询所有数据记录
select* from student where name like '%%'


八、ORDER BY

//单字段升序(默认)排列
select * from student order by grade
//单字段降序排列
select * from student order by grade desc
//多字段排序
select * from student order by grade,date desc


九、LIMIT(常用于分页)

select * from  student LIMIT(a,b)
//a为起始位置,B表示查询的条数
select * from  student LIMIT b
//不指定起始位置则从第一条开始


MySql所支持的统计函数,如果所操作的表中没有任何数据记录, count函数返回数据为NULL 其余返回为0

十、COUNT

//统计所有数据记录的条数,不管是不是NULL
select count(*) allNumber from student
//NULL的不统计
select count(course) partNumber from  student


十一、AVG统计平均值

SUM求和

MAX、MIN最大最小值

十二、GROUP BY

//
select * from student group by stuClass


十三、GROUP_CONCAT

十四、

十五、

十二、

十二、

十二、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: