zoukankan      html  css  js  c++  java
  • MySQL(增删改查补充)

    SQL语句数据行操作补充
                create table tb12(
                    id int auto_increment primary key,
                    name varchar(32),
                    age int
                )engine=innodb default charset=utf8;
        
            增
                insert into tb11(name,age) values('alex',12);
                
                insert into tb11(name,age) values('alex',12),('root',18);     #同时增加多条
                
                insert into tb12(name,age) select name,age from tb11;         #将tb11表整个插入tb12
            删
                delete from tb12;
                delete from tb12 where id !=2  
                delete from tb12 where id =2  
                delete from tb12 where id > 2  
                delete from tb12 where id >=2  
                delete from tb12 where id >=2 or name='alex'
            
            改
                update tb12 set name='alex' where id>12 and name='xx'      #条件
                update tb12 set name='alex',age=19 where id>12 and name='xx'
            查
                
                select * from tb12;
                
                select id,name from tb12;
                
                select id,name from tb12 where id > 10 or name ='xxx';
                
                select id,name as cname from tb12 where id > 10 or name ='xxx';    #将name取别名显示
                
                select name,age,11 from tb12;  #第三列全为11
                
                其他:
                    select * from tb12 where id != 1
                    select * from tb12 where id in (1,5,12);  # 1 or 5 or 12
                    select * from tb12 where id not in (1,5,12);
                    select * from tb12 where id in (select id from tb11)  #范围克重另一张表中选择
                    select * from tb12 where id between 5 and 12;  #闭区间
        
                
                    通配符:
                    
                    select * from tb12 where name like "a%" #以a开头的        %a以a结尾的
                    select * from tb12 where name like "a_" #a后边只有一个位置
        
                
                    分页:
                    
                        select * from tb12 limit 10;    #分页显示
                        select * from tb12 limit 0,10;
                        select * from tb12 limit 10,10;
                        select * from tb12 limit 20,10;   #起始位置,和显示数量    从20个开始,往后显示10个                    
                        select * from tb12 limit 10 offset 20;
                        从第20行开始读取,读取10行;
            

                    
                    排序:
                        select * from tb12 order by id desc; 大到小
                        select * from tb12 order by id asc;  小到大
                         select * from tb12 order by age desc,id desc;
                          
                        取后10条数据    id从大到小排后取前十个
                        select * from tb12 order by id desc limit 10;

    补充:
                    左右连表: join
                    上下连表: union
                            # 自动去重
                            select id,name from tb1
                            union
                            select num,sname from tb2
                            
                            # 不去重
                            select sid,sname from student
                            UNION ALL
                            select sid,sname from student

  • 相关阅读:
    927小程序繁星计划峰会 · 看完这七大话题 你会更了解阿里小程序
    不吹不黑,今天我们来聊一聊 Kubernetes 落地的三种方式
    虽然他们说是水题,但我觉得思想蛮好的
    新学dfs(看懂了)
    01背包,死记硬背(我是真的蠢)
    装箱问题(太笨、还没想通)
    高精度乘法,string中的坑
    双十一用python秒杀京东好货!
    高精度减法用string 和 stack
    n阶汉诺塔 记住吧。。
  • 原文地址:https://www.cnblogs.com/112358nizhipeng/p/9949765.html
Copyright © 2011-2022 走看看