排序
order by
asc 默認(rèn)
desc
單列排序
SELECT * FROM 表明 ORDER BY age (desc|asc);
條件排序
SELECT* FROM 表明 WHERE 條件 ORDER BY age (asc|desc);
多列排序
SELECT * FROM studentinfo order by age desc,gender asc;
有一樣的值在相同的時(shí)候灯抛,第二個(gè)條件進(jìn)行排序。
列子:age相同音瓷,就把gender的值做升許对嚼。
英文漢字按AZ排序
如果排序的列是中文,排序的列編碼轉(zhuǎn)為gbk绳慎,然后進(jìn)行排序纵竖。
SELECT * FROM biao ORDERBY convert(name USING gbk) DESC;
列名
限定查詢
如果linit后面只跟了一個(gè)數(shù)字,表示0杏愤,數(shù)字靡砌。表示限制返回多少條。
SELECT * FROM biao ORDER BY LIMIT 6;
SELECT * FROM biao ORDER BY LIMIT 0珊楼,6;
查東西限定乏奥,要在查到結(jié)果以后進(jìn)行限定,所以limit 一直放在后面。
第一位偏移量亥曹,也叫開頭(不包含當(dāng)前的行)邓了。后一個(gè)表示限制返回多少條。
SELECT * FROM biao ORDER BY LIMIT 6媳瞪,6;
從第6開始查骗炉,返回6條。返回7-12行蛇受。
如何實(shí)現(xiàn)分列句葵。
輸入頁(yè)碼m 返回20條內(nèi)容。
SELECT * FROM stu LIMIT (m-1)*20,20;
取年齡最小
SELECT * FROM stunt ORDER BY age LIMIT1;
聚合函數(shù)
AVG 平均值
SELECT AVG(age) FROM stu;
別名:
SELECT AVG(age) as avgage FROM stu;
COUNT 統(tǒng)計(jì)某一列項(xiàng)的總行數(shù)
SUM 計(jì)算列總和
MIN 某一列最小值
MAX 某一列最大值
組合:
mysql> SELECT COUNT(age),MAX(age) FROM studentinfo;
去重:
mysql> SELECT COUNT(DISTINCT age) FROM studentinfo;
添加條件:
SELECT COUNT(gender) FROM studentinfo WHERE gender=1;
分組
GROUP BY (相同放在一組)
丹丹分組無(wú)意義:
SELECT age from studentinfo GROUP BY age;
加聚合(age 每組有多少人):
SELECT count(*),age from studentinfo GROUP BY age;
GROUP 與 group_concat(列):
SELECT gender,group_concat(student_name),group_concat(age) from studentinfo GROUP BY gender;
+--------+-------------------------------------------+-------------------+
| gender | group_concat(student_name) | group_concat(age) |
+--------+-------------------------------------------+-------------------+
| 0 | xx,老黃,小環(huán),小花花,小剛,小牛 | 23,23,6,22,20,25 |
| 1 | jim,王二 | 30,11 |
+--------+-------------------------------------------+-------------------+
group by +with rollup
在最后新增一行兢仰,來(lái)記錄當(dāng)前列里所有記錄的總和
SELECT gender,group_concat(student_name),group_concat(age) from studentinfo GROUP BY gender with rollup;
HAVING
根據(jù)條件過(guò)濾(篩選)結(jié)果乍丈。
HAVING和WHERE相似。where是根據(jù)條件篩選結(jié)果把将,不過(guò)having根據(jù)條件篩選結(jié)果轻专,是在已有結(jié)果基礎(chǔ)進(jìn)一步篩選。
如何使用:
原句:
SELECT count(),age from studentinfo group by age;
進(jìn)行過(guò)濾:
分組行數(shù)大于等于2才返回察蹲。
SELECT count(),age from studentinfo group by age having count(*)>=2;
where行
having 和group組合请垛,在組基礎(chǔ)上篩選
SELECT count(*),age,gender from studentinfo group by age,gender having gender=1 order by age desc;
order by 配合使用:
SELECT count(*),age,gender from studentinfo group by age,gender having gender=1 AND age>23 order by age desc;
limit配合使用:
SELECT count(*),age,gender from studentinfo group by age,gender having gender=1 AND age>23 order by age desc LIMIT 2;
查詢順序:
select
列洽议,列
from
表名
where
條件
group∽谑铡by
列,列
having
條件
order⊙切帧by
列』旎(asc|desc)
limit start审胚,count
r原樣輸出
str = '\c' \c
str = 'r\c' \c
\b單詞的邊際分割
三大約束
none
not⌒傺null
唯一 unique:
設(shè)置唯一以后菲盾,當(dāng)插入相同的唯一時(shí)就報(bào)錯(cuò)颓影。
唯一可為null
主鍵 不能為空
創(chuàng)表跟primary類似。
CREATE TABLE unique5 ( userid int , UNIQUE KEY(userid) );
result
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| userid | int(11) | YES | UNI | NULL | |
+--------+---------+------+-----+---------+-------+
主鍵
mysql> CREATE TABLE IF NOT EXISTS unique2 ( userid int not null, PRIMARY KEY(userid) );
Query OK, 0 rows affected (0.33 sec)
mysql> desc unique2;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| userid | int(11) | NO | PRI | NULL | |
+--------+---------+------+-----+---------+-------+
【數(shù)據(jù)庫(kù)五大約束】
1.primary KEY:設(shè)置主鍵約束懒鉴,不以為空诡挂,唯一;
2.UNIQUE:設(shè)置唯一性約束临谱,不能有重復(fù)值璃俗,可以為null;
3.DEFAULT 默認(rèn)值約束悉默,height DOUBLE(3,2)DEFAULT 1.2 height不輸入是默認(rèn)為1,2城豁,不使用就會(huì)使用默認(rèn)值。
4.NOT NULL:設(shè)置非空約束抄课,該字段不能為空不等于空字符串唱星;
5.FOREIGN key :設(shè)置外鍵約束雳旅,。