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

  • 相关阅读:
    WinCE 编译固定IP到内核
    wince telnet登陆密码的取消
    lab 美国大学实验室
    PLC 开放性源代码的软件
    linux 嵌入式Linux下3G无线上网卡的驱动
    创业者/职业经理人/员工受用的36句感悟
    iShoot Developer Makes $600,000 In One Month
    如何彻底解决Vista狂读硬盘的问题
    宁做创业狼,不做打工狗
    跟我学EJBCA系列一:安装
  • 原文地址:https://www.cnblogs.com/112358nizhipeng/p/9949765.html
Copyright © 2011-2022 走看看