数据库的查询

数据库的查询:
一、
【1】数据库的,增加内容
(1)insert into 表名 values(要添加的数据值'',''.....'');

(2)insert into 表名 (列名1,列名2) values('列1对应的数值','列2对应的数值');


【2】数据库的,删除数据
delete from 表名 where 某列 = '数据'
条件

【3】数据库的,修改数据
update 表名 set Name='李四' where Code='p001'

二、查询数据

【1】简单查询
select * from Info

例1 查询全体学生的学号与姓名 

SELECT Sno,Sname FROM Student; 
<目标列表达式> 中各个列的先后顺序可以与表中的顺序不一致。

 

select Code as '代号',Name as '姓名' from Info

 【2】条件查询

select * from Car where Code='c002'
select * from Car where Brand='b001' and Powers=130 或者用or      and两个条件瞒足      or并且,瞒足一条件    

【3】模糊查询
select * from Car where Name like '%奥迪%'        %代表任意多个字符 _代表一个字符

【4】排序查询
select * from 表名 order by 列名1,Powers desc 降序       cas升序

【5】范围查询
select * from Car where Price>=40 and Price<=60
select * from Car where Price between 40 and 50

【6】离散查询
select * from Car where Code in ('c001','c003','c005','c007')
select * from Car where Code not in('c001','c003','c005','c007')

【7】聚合函数,统计查询
select sum(Price) from Car #查询所有价格之和 sum()求和
select count(Code) from Car #查询数据条数
select max(Code) from Car #求最大值
select min(Brand) from Car #求最小值
select avg(Price) from Car #求平均值

【8】分页查询
#每页显示5条数据,取第2页的数据
select * from Car limit (n-1)*5,5

【9】去重查询
select distinct Brand from Car

【10】分组查询
select count(*),Brand from Car group by Brand
select Brand from Car group by Brand having count(*)>3 #分组之后根据条件查询使用having 不使用where

 

 

select * from 表名
select 列名1,列名2... from 表名 --投影
select * from 表名 where 条件 --筛选

 

原文地址:https://www.cnblogs.com/yuyu1993/p/5539841.html