数据库表的增删改查用得最频繁最广泛的就是查询,对于单表主要知识点有:区别重复数据,数据实现简单运算,条件查询,数据排序,限制数据记录查询数量,统计函数。
select * from table_name;
select 字段1,字段2,... from table_name;
select distinct 字段1,字段2,... from table_name;
+:加法;
-:减法;
*:乘法;
/(div):除法;
%(mod):求余;
以下是我的表结构:
Field Type Null Key Default Extra
-------- ------------- ------ ------ ------------ ----------------
id int(11) NO PRI (NULL) auto_increment
bname varchar(100) NO (NULL)
author varchar(10) NO (NULL)
number int(11) NO (NULL)
category varchar(20) NO (NULL)
abstract varchar(1000) NO (NULL)
id bname author number category abstract
------ ---------------- ---------- ------ --------- -----------------------------------
1 java编程思想 Bruce Ecke 100 计算机 java神书,强烈推荐
2 线性代数 李凤霞 120 数学 大学公共课线性代数教材
3 苏菲的世界 Jostein Ga 78 哲学 哲学启蒙读本,强烈推荐
加法:SELECT number+5 FROM t_book;
减法:SELECT number+5 FROM t_book;
乘法:SELECT number*5 FROM t_book;
除法:SELECT number/5 FROM t_book;
求余:SELECT number%5 FROM t_book;
也可以使用别名方式使得显示更优雅更直观:
select number*5 count_book from t_book;
select number*5 ‘数量乘以五’ from t_book;
语法:select field1 [AS] otherfield1,field2 [AS] otherfield2,fieldn [AS] otherfieldn from table_name;
SELECT CONCAT(bname,'这本书的类别是:',category) AS '描述' FROM t_book;
大于:>
小于:<
等于:=
不等于:!= (<>)
大于等于:>=
小于等于:<= tips:在mybatis中,这个<=是与它一个符号冲突的,所以需要告诉他这个是代表我们的“小于等于”->
select bname from t_book where id>2;
AND(&&) 逻辑与
OR(||) 逻辑非
XOR 逻辑异或
NOT(!)
SELECT * FROM t_book WHERE id>1 OR number>90;
SELECT * FROM t_book WHERE id>1 XOR number>90;
NOT:还不会用,如果有知道的同学欢迎评论留言,感激
SELECT * FROM t_book WHERE number BETWEEN 90 AND 110;
SELECT * FROM t_book WHERE number NOT BETWEEN 90 AND 110;
SELECT * FROM t_book WHERE number IN(1,2,67,78,89,100,234,456);
SELECT * FROM t_book WHERE abstract LIKE '%强烈%';
%:通配符
SELECT * FROM t_book ORDER BY number ASC;
SELECT * FROM t_book ORDER BY number DESC;
SELECT * FROM t_book ORDER BY id DESC,number ASC;
SELECT * FROM t_book LIMIT 2;
只显示 2 条数据(一共有三条数据,显示id=1,id=2)
语法:select field1,field2,fieldn from table_name where condition limit offset_start,row_count;
例如:SELECT * FROM t_book LIMIT 2,1;
结果:显示了 id=3 的那 1 条数据。起始偏移量与数组一样,从 0 开始
COUNT():统计表中所选字段的记录条数,【COUNT(*) | 不忽略null值、COUNT(field) | 忽略null值】;
AVG():统计表中所选字段的平均值,忽略null值;
SUM():计算所选字段的总和,忽略null值;
MAX():找出所选字段的最大值,忽略null值;
MIN():找出所选字段的最小值,忽略null值;
group by:分组关键字
SELECT SUM(number) FROM t_book;
SELECT MAX(number) FROM t_book;
SELECT * FROM t_book GROUP BY number;
语法:SELECT GROUP_CONCAT(field) FROM table_name WHERE CONDITION GROUP BY field;
SELECT bname,GROUP_CONCAT(abstract) FROM t_book WHERE id<3 GROUP BY author;
语法:SELECT GROUP_CONCAT(field),function(field) FROM table_name WHERE CONDITION GROUP BY field1,field2,fieldn;
SELECT bname,GROUP_CONCAT(abstract),AVG(number) FROM t_book WHERE id<3 GROUP BY author,bname;
如果想实现对分组进行条件限制,不能通过 where 来实现,因为 where 主要用来实现条件限制分组数据记录。官方专门提供了 HAVING 来实现条件限制分组数据记录。
语法:SELECT function(field) FROM table_name WHERE CONDITION GROUP BY field1,field2,fieldn HAVING CONTITION;
SELECT bname,GROUP_CONCAT(abstract),AVG(number) FROM t_book WHERE id<3 GROUP BY author,bname HAVING AVG(number)>100;
SELECT bname,GROUP_CONCAT(abstract),AVG(number) FROM t_book WHERE id<3 GROUP BY author,bname HAVING COUNT(number);