您的位置:首页 > 数据库 > MySQL

初学mysql(二)-数据库之表内容单表查询

2016-11-06 16:40 465 查看
        上一篇博客说了数据库与数据库中表的操作。这一篇中我来说一下我学习的对数据库中的表的内容的操作,其中包括表容查询,增加,删除,某一字段求和,求平均值,求最大最小值,以及一些关键字的用法。

selelct查询表:最简单的查询所有字段:

select * from t_book;


指定字段的查询:

select id, bookName,author from t_book;


select bookName 书名,author 作者,price 价格from t_book where id = 1;

b: in关键字指定的where关键字指定属性字段值得查询查询:

select bookName 书名,author 作者,price 价格 from t_book where id (not) in(1,2,3);

c: between and 关键字指定的查询:

select * from t_book where id (not)between 1 and 3;

d: 模糊查询:

select * from t_book where bookName like '%java%';%多个字段匹配;

e: 一个字段的匹配:

select * from t_book where bookName like '_Java_';(第一个字符和第三个字符任意字符中间那个字为Java);

f: 空值(不为空)查询:

select * from t_book where bookName is (not) null;

g: 多条件查询:与查询用and连接:

select * from t_book where bookName = 'Java编程思想' and author = '埃克尔';

h: 或查询用or连接:

select * from t_book where bookname = 'java编程思想' or author='乔治';

i: distinct去重复查询(去掉bookName中重复的书名):

select distinct bookName from t_book;

j: 对某个字段进行排序ASC升序排序,Desc降序排序:

select * from t_book order by id  asc(desc)

k: order by对查询结果集按某一字段排序;asc:按字段升序排列;desc:按字段降序排列:

select * from t_book order by id desc(asc);

l: limit分页查询:

select bookName, bookNum from t_book limit 2, 2

m: group by 分组查询: 

SELECT * FROM t_book GROUP BY bookName HAVING bookNum > 3;


group by常常和一些聚合函数一起使用

    
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: