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

  • 相关阅读:
    PLC 控制系统资源
    .net core 控制台程序生成EXE
    EF Core 日志跟踪sql语句
    Oracle 设置自启动
    数据库批量插入数据
    字符串面试题系列之六:在字符串中删除特定的字符
    字符串面试题系列之五:删除字符串空格
    字符串面试题系列之四:字符串匹配二
    字符串面试题系列之三:左旋转字符串
    字符串面试题系列之二:连续最长数字串
  • 原文地址:https://www.cnblogs.com/112358nizhipeng/p/9949765.html
Copyright © 2011-2022 走看看