SQL语句的基本使用
我们要学会使用查询关键字select(这里给大家提醒一下SQL语句是不区分大小写的哦)+(要查询的内容(如果要查询全部内容用*号)比如姓名:name,等等)+from+(表名) 来查询表中的所有数据,以上就是基本用法
接下介绍如何修改查询选项的名称:
select name as ”这是修改后的名字 ” form books
where的使用
这个词呢我通常理解为选项或者叫执行条件,请看:
select name as "书 名" from books--别名中有空格的情况
select
id,name,stock,publishedby
from
books
where
id=5 >,<,<=,>=,!=,<>
这里的where就作为查询的执行条件使用的,很好理解的
还可以这样
where
id=3 or id=7 or id=8
或者
where
id in (3,7,8)
还有同时满足多个条件
where
id>3 and stock <10
在sql中使用单引号来表示字符串
select *
from books
where name='妈妈'
模糊查询使用_匹配单个字符,使用%匹配若干个字符
select *
from books
where name like '_好' //where name like’_好%’
6.查询结果以price(价格)的升序排序,降序把asc改为desc
select *
from books
where id >3
order by price asc (升序)
计数
select count(id)
from books
7.平均值,总和
select avg(price),sum(price)
from books
8.去重
select distinct(publishedby)
from books
9.显示部分结果,第一个参数是从第几个开始(下标从0开始),第二个参数是显示多少个数据。
select *
from books
limit 3,3
10.使用group by把结果进行分组
select count(id),publishedby
from books
group by publishedby(出版社)
11.使用having来筛选group by 的某个组
select avg(price),sum(price),publishedby
from books
group by publishedby
having publishedby = '人民出版社'