数据库查询操作

https://www.cnblogs.com/zhou0000/p/8287520.html

查询语句语法

SELECT

    [ DISTNCE ] 显示字段列表

    [ FROM ] 单表或多表关联    

    [ WHERE ] 行记录过滤条件

    [ GROUP BY ] 分组字段

    [ HAVING ] 条件

    [ ORDER BY ] 排序字段

    [ LIMIT ] 保留记录或区间

例一:查询所有成绩表信息:select * from t_score

            查询所有学生及其考试科目信息(不去重):select name,subject from t_score

            字段别名:select name 姓名,subject as 科目 from t_score (列明_别名或列明 as 别名)

            distinct 去重 :select distinct name,subject from t_score

            where单条件:select * from t_score where class = '一班'

                                    select * from t_score where not class = '一班'

            where多条件(and):select * from t_score where class = '一班' and subject = '语文'

            where多条件(or)select * from t_score where subject = '数学' or subject = '语文'

                            等价于:select * from t_score where subject in( '数学' , '语文')

            where多条件(like):

                         select * from t_score where name like "张%"(以张为开头的)

                         select * fromt_score where name like "%红杏"(以红杏结尾的)

                         select * fromt_score where name like "%红%"(包含红的)

            group by:

                        求平均成绩:select name,avg(score)as 平均 from t_score group by name

                        常用的聚合函数; 求和sum()

                                                       最大/最小:max()/min()

                                                       记录条数:count(*)

                                                        平均avg()

            having,用于对中间结果进行过滤:

                            查询平均成绩不低于85分的学生及其成绩

                             select name,avg(score)as 平均 from t_score group by name                                                      having avg(score)>=85

            order by:

                    查询成绩信息,要求按照科目名称降序排序

                    select * from t_score order by subject desc

                    desc------降序排列、    

                     asc-------升序排列

              order by 多字段

                     select * from t_score order by subject desc,score asc

            limit :

                select name,score from t_score where subject = '语文' order by score desc limit 3

复杂查询语句:

select  name, avg(score) as 平均 from t_score where class = '一班' group by name having avg(score)>=85 order by avg(score) desc



                        

你可能感兴趣的:(数据库查询操作)