SQL学习——查询基础

来源:互联网 发布:知党规 行敬畏 编辑:程序博客网 时间:2024/04/30 08:04

查询基础:(关键词:select)

简单查询:

1:查询表的全部行和列

例1:select user_qq,user_name,user_sex from users;

例2:select * from users;

2:查询表的部分列

例:select user_qq from users;

3:使用别名

例:select user_qq [as] '玩家QQ' ,user_name [as] '玩家姓名' from users;

4:去除结果中的重复行

例:select distinct user_qq from scores;

5:指定显示范围

例:select * from users limit 2,3;      (代表显示第3,4,5条数据)

select * from users limit 3;                 (代表显示第1,2,3条数据)


条件查询:

select 列名 from 表名 where 表达式

例:select * from users where user_qq='1234';

<>   不等于    and   与     or     或      not     非


模糊查询:(关键词:between and)

例:select * from scores where score between 2500 and 3000;

between and 中前面的数比后面的数小

not betwween and 表示不在该范围


通配符:

例:select * from users where user_name like '孙%'

%匹配任意一个或多个字符

not like       


查询空值:

例:select * from users where user_birthday is null;

is not null 


对查询结果排序:

select 列名 from 表名 where gno=1 order by 列名 [asc|desc]   (asc为升序(默认),desc为降序)

多列排序:

select * from scores order by gno asc,score desc;                 (先排gno再基于gno基础上排score)

0 0
原创粉丝点击