查詢基礎:(關鍵詞: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)
新聞熱點
疑難解答