zoukankan      html  css  js  c++  java
  • MySQL数据库—查询基础,简单查询,条件查询,对查询结果排序

    一.SELECT语句 SELECT COL1,COL2,....COLn FROM TABLE1,TABLE2,....TABLEn

    [WHERE CONDITIONS] -- 查询条件

    [GROUP BY GROUP_BY_LIST] -- 查询结果分组

    [HAVING CONDITIONS] -- 查询条件-统计结果作为条件

    [ORDER BY ORDER_LIST[ASC|DESC] -- 查询结果排序

    二.简单查询

    1.查询表的全部行和列

    eg:查询玩家表中全部的行和列

     select  user_qq,user_name,user_sex,user_birthday,user_mobile from users;

     select * from users;

    2.查询表的部分列

    eg:从玩家表中查询玩家QQ和昵称

    select user_qq,user_name from users;

    3.别名的使用

    eg:从玩家表中查询玩家QQ和昵称,并显示为‘玩家QQ' 和 '玩家昵称'

    select user_qq as '玩家QQ',user_name as '玩家昵称' from users;

    select user_qq '玩家QQ',user_name '玩家昵称' from users;

    4.DISTINCT关键字 -消除结果集中的重复行

    eg:显示参与了游戏的玩家QQ,要求参与了多个游戏的玩家不重复显示QQ

    select distinct user_qq from scores;

    5.LIMIT关键字 -指定结果集中数据的显示范围

    eg:显示玩家表中第3至第5条数据

    select * from users limit 2,3;

    select*from users limit 3 ---只显示前三条数据

    三.条件查询

    1.普通条件查询

    语法:SELECT COL_LIST FROM TABLE_NAME [WHERE CONDITION_EXPRESSION]

    eg1:查询QQ号为12301的玩家信息

    select * from users where user_qq =12301;

    eg2:查询分数大于2500分的数据

    select *from scores where score>2500;

    <> -----不等于    >= -----大于等于    <=  -----小于等于

    eg3:查询游戏编号为1且分数大于4000分的分数信息

    select * from scores where gno=1 and score>4000;

    逻辑运算符:并且 -- and

                     或者 -- or

                     非   -- not

    eg4: 查询游戏编号为1和2的分数信息

    select * from scores where gno=1 or gno=2;

    2.模糊查询

    eg1:查询分数在2500(含)到3000(含)的分数

    select *from scores where score>=2500 and score<=3000;

    select * from scores where score between 2500 and 3000;

    eg2:查询分数不在2500(含)到3000(含)的分数信息

    select * from scores where score not between 2500 and 3000;

    eg3:查询1987年1月1日到1992年7月31日出生的玩家

    select * from users where user_birthday between '1987-01-01' and '1992-0731';

    通配符: '_'    一个字符     Branch like 'L_'

                %     任意长度     Route_Code Like 'AMS-%'

                []     指定范围内   Airbusno Like 'AB0[1-5]'

                [^]   不在括号中   Airbusno Like 'AB0[^]'

    eg4:查询所有姓孙的玩家信息

    select * from users where user_name like '孙%';

    eg5:查询所有非姓孙的玩家信息

    select * from users where user_name not like '孙%';

    3.查询空值得运算符

    eg:查询生日为空的null的玩家信息

    select * from users where use_birthday is null;

    eg:查询生日不为NULL的玩家信息

    select * from users where user_birthday is not null;

    四 对查询结果排序

    1. 对指定列进行排序(排序依据,排序方式)

    语法:SELECT CLO_LIST FROM TABLE_NAME ORDER BY ORDER_BY_LIST [ASC/DESC]

    例:查询分数表中编号为1的所有分数信息,并按照分数升序排序

    select *from scores where gno=1 order by score asc.

    例:查询分数表中编号为1的所有分数信息,并按照分数降序排序

    select * from score where gno=1 order by score desc.

    2. 对多列进行排序(排序依据,排序方式,优先级)

    例:查询分数表中的所有信息,并按照游戏编号的升序和分数的降序进行排序

    select * from scores order by gno asc, score desc

  • 相关阅读:
    Navicat 创建mysql存过、定时执行存过
    windows 系统 MySQL_5.6.21安装教程
    ldf和mdf文件怎么还原到sqlserver数据库
    免安装的tomcat转服务
    关掉IE提示“当前安全设置会使计算机有风险”
    U盘制作系统启动盘方法
    Tomcat窗口标题,中文乱码解决方法
    MyEclipse10安装SVN插件
    IE浏览器的卸载操作
    739. Daily Temperatures 每日温度
  • 原文地址:https://www.cnblogs.com/jly144000/p/7384794.html
Copyright © 2011-2022 走看看