zoukankan      html  css  js  c++  java
  • mysql—增删改查语句总结

      1 关于MySQL数据库——增删改查语句集锦
      2 一、基本的sql语句
      3 
      4 CRUD操作:    
      5 create 创建(添加)
      6 read 读取
      7 update 修改
      8 delete 删除
      9 
     10 1、添加数据
     11 insert into Info values('p009','张三',1,'n001','2016-8-30 12:9:8') ; 
     12 给特定的列添加数据
     13 insert into Info (code,name) values('p010','李四');
     14 自增长列的处理
     15 insert into family values('','p001','数据','T001','数据',1);
     16 
     17 insert into 表名 values(值)
     18 
     19 2、删除数据
     20 删除所有数据
     21 delete from family
     22 删除特定的数据
     23 delete from Info where code='p001'
     24 
     25 delete from 表名 where 条件
     26 
     27 3、修改数据
     28 修改所有数据
     29 update Info set name='徐业鹏' 
     30 修改特定数据
     31 update Info set name='吕永乐' where code='p002' 
     32 修改多列
     33 update Info set name='吕永乐',sex=1 where code='p003' 
     34 
     35 update 表名 set 要修改的内容 where 条件  tno =
     36 
     37 4、读取数据
     38 (1)简单读取,查询所有列(*)  所有行(没有加条件)
     39 select * from Info
     40 (2)读取特定列
     41 select code,name,class from Info
     42 (3)条件查询
     43 select * from Info where code='p003'
     44 (4)多条件查询
     45 select * from Info where code='p003' or nation='n002' #或的关系
     46 select * from Info where sex=0 and nation='n002' #与的关系
     47 (5)关键字查询(模糊查询)
     48 查所有包含奥迪的汽车
     49 select * from car where name like '%奥迪%'; #百分号%代表任意多个字符 
     50 查以'皇冠'开头的所有汽车
     51 select * from car where name like '皇冠%';
     52 查询汽车名称中第二个字符是'' 53 select * from car where name like '_马%'; #下划线_代表任意一个字符
     54 (6)排序查询
     55 select * from car order by powers  #默认升序排列
     56 select * from car order by powers  #升序asc 降序 desc
     57 先按brand升序排,再按照price降序排
     58 select * from car order by brand,price desc
     59 
     60 
     61 (7)范围查询
     62 select * from car where price9()>40 and price<60
     63 select * from car where price between 40 and 60
     64 
     65 (8)离散查询
     66 select * from car where price=30 or price=40 or price=50 or price=60;
     67 select * from car where price in(30,40,50,60)取出数据
     68 select * from car where price not in(30,40,50,60)去掉数据
     69 
     70 (9)聚合函数(统计查询)
     71 select count(*) from car
     72 select count(code) from car #取所有的数据条数
     73 select sum(price) from car #求价格总和
     74 select avg(price) from car #求价格的平均值
     75 select max(price) from car #求最大值
     76 select min(price) from car #求最小值
     77 
     78 (10)分页查询
     79 select * from car limit 0,10  #分页查询,跳过几条数据(0)取几条(10)
     80 规定一个每页显示的条数:m
     81 当前页数:n]
     82 select * from car limit (n-1)*m,m
     83 
     84 (11)去重查询
     85 select distinct brand from car
     86 
     87 (12)分组查询
     88 查询汽车表中,每个系列下汽车的数量
     89 select brand,count(*) from car group by brand
     90 分组之后,只能查询该列或聚合函数
     91 
     92 取该系列价格平均值大于40的系列代号
     93 select brand from car group by brand having(加条件) avg(price)>40
     94 
     95 取该系列油耗最大值大于8的系列代号
     96 select brand from car group by brand having max(oil)>8
     97 
     98 
     99 二、MySql的高级查询(使用外连接)
    100 
    101  连接查询
    102 SELECT t1.Name,t2.Brand_Name FROM brand t2,car t1 -- 笛卡尔乘积
    103 WHERE t2.Brand = t1.Brand
    104 
    105 
    106 -- 多表连接查询
    107 SELECT t1.Name,t2.Brand_Name,t3.prod_name  FROM car t1 LEFT JOIN brand t2 ON t1.Brand = t2.Brand
    108 
    109 LEFT JOIN productor t3 ON t2.Prod = t3.Prod
    110 
    111 
    112 -- 联合查询 字段数必须一样
    113 SELECT `Name`,Price FROM car UNION SELECT Brand_Name,Brand_Memo FROM brand
    114 
    115 
    116 -- 子查询(***)
    117 SELECT * FROM car WHERE car.brand in (SELECT Brand FROM brand WHERE Prod = 'p001')
    118 
    119 说明:使用外连接
    120 
    121 A、leftouterjoin122 左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
    123 SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
    124 B:rightouterjoin:
    125 右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
    126 C:full/crossouterjoin127 全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
    128 D:分组:Group by:
    129   一张表,一旦分组完成后,查询后只能得到组相关的信息。
    130  组相关的信息:(统计信息) count,sum,max,min,avg  分组的标准)
    131   在SQLServer中分组时:不能以text,ntext,image类型的字段作为分组依据
    132  在selecte统计函数中的字段,不能和普通的字段放在一起;
    133 E:外连接查询(表名1:a 表名2:b)
    134 select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c
    135 F:between的用法,between限制查询数据范围时包括了边界值,not between不包括
    136 select * from table1 where time between time1 and time2
    137 select a,b,c, from table1 where a not between 数值1 and 数值2
    138 G:四表联查问题:
    139 select * from a left inner join b on a.a=b.b right inner join c on a.a=c.c inner join d on a.a=d.d where .....
    140 H::前10条记录
    141 select top 10 * form table1 where 范围
    142 I:选择在每一组b值相同的数据中对应的a最大的记录的所有信息(可以用于论坛每月排行榜,每月热销产品分析,按科目成绩排名,等等.)
    143 select a,b,c from tablename ta where a=(select max(a) from tablename tb where tb.b=ta.b)

     

  • 相关阅读:
    Hadoop编译源码
    大数据生态体系
    crontab任务调度
    yum概述配置
    如何识别图像边缘
    神经网络入门
    Linux命令locate
    B1299 [LLH邀请赛]巧克力棒 博弈论
    B1090 [SCOI2003]字符串折叠 区间dp
    B1068 [SCOI2007]压缩 区间dp
  • 原文地址:https://www.cnblogs.com/chenleixing-2017/p/7647828.html
Copyright © 2011-2022 走看看