mysql中各类复杂的增删改查

1.mysql查出数据表中连续出现三次或三次以上的数据

建一张表以下:表名为 numbermysql

1.1 要求找出num列连续出现三次或三次以上的数据:

select * from number where id in (
    select distinct n1.id from number n1,number n2,number n3
    where (n1.num = n2.num and n2.num = n3.num and ( 
         (n1.id + 1= n2.id  and n2.id +1 = n3.id)or
         (n3.id + 1= n2.id  and n2.id +1 = n1.id)or
         (n3.id + 1= n1.id  and n1.id +1 = n2.id)
      )
    ) 
order by n1.id )

 

运行结果:sql

 

1.2找出上表(运行结果表)中重复数据中id最小的数据。

思想:将上表中的数据分组取最小。微信

select min(id) as minid,id,num
from ((select * from number where id in (
    select distinct n1.id from number n1,number n2,number n3
    where (n1.num = n2.num and n2.num = n3.num and ( 
         (n1.id + 1= n2.id  and n2.id +1 = n3.id)or
         (n3.id + 1= n2.id  and n2.id +1 = n1.id)or
         (n3.id + 1= n1.id  and n1.id +1 = n2.id)
      )
    ) 
order by n1.id ))as t) group by num ;

 

运行结果:学习

2.删除表中的重复数据,保留id最小的数据

原表中的数据以下:spa

/*
    方法一:
        1.建立临时表
        2.将须要的数据保存到临时表
        3.清空原表,将临时表中的数据保存到原表中
        4.删除临时表
*/
    create temporary table temp

    select min(id),email from test group  by email;

    truncate table test;

    insert into test select * from temp;

    select * from test;

    drop table temp;

/*
  方法二:
        1.按照邮件分组,把每一组中的最小的id取出放在临时表中
        
        2.删除原表中除临时表中id之外的其余id 对应的数据
*/

    create temporary table temp

    select min(id) as minid from test group by email;  

    delete from test where id not in (select minid from temp);

    drop table temp;

/*
  方法三:
       在原表上操做
*/
   delete from test where id not in (
       select minid from (
          select min(id) minid from test group by email
         )b
     );
     select * from test;

运行结果:3d

3.mysql中的各类链接

建立两张表,表明两个店铺,每一个店铺有不一样的,每一个柜台卖不一样的商品。code

/*
   两个商店,每一个店有不一样的柜台,每一个柜台上卖有不一样的商品
*/
create table tableA(
    id int(10) not null primary key auto_increment,
        monorail varchar(20),
        variety varchar(50)
);
insert into tableA 
values (1,'a1','苹果'),(2,'a1',''),(3,'a3','香蕉'),(4,'a4','苹果'),(5,'a5','西瓜'),
(6,'a6','苹果'),(7,'a7','葡萄'),(8,'a8','桃子');
create table tableB(
    id int(10) not null primary key auto_increment,
        monorail varchar(20),
        variety varchar(50)
);
insert into tableB 
values (1,'b1',''),(2,'b2','猪肉'),(3,'b3','苹果'),(4,'b4','西瓜'),(5,'b5',''),
(6,'b6','香蕉');

两个表数据以下:blog

 

rem

3.1.内链接

/*
  内链接,查处两个商店共有的商品
*/
select * from tablea A INNER  JOIN tableb B
on A.variety = B.variety ;

 运行结果io

 

3.2.左链接

/*
  左链接,以A表为左表
*/
select * from tablea A LEFT  JOIN tableb B
on A.variety = B.variety ;

运行结果

3.3.右链接

/*
  右链接,以A表为左表
*/
select * from tablea A RIGHT  JOIN tableb B
on A.variety = B.variety ;

运行结果

 

3.4.左外链接

/*
  左外链接,以A表为左表
*/
select * from tablea A LEFT  JOIN tableb B
on A.variety = B.variety Where B.variety is null;

运行结果

3.5.右外链接

/*
  右外链接,以A表为左表
*/
select * from tablea A RIGHT  JOIN tableb B
on A.variety = B.variety Where A.variety is null;

运行结果

3.6.全链接  

mysql不支持full  (outer) join 故使用union代替

/*
  全链接
*/
select * from tablea A LEFT  JOIN tableb B
on A.variety = B.variety ;
union
select * from tablea A RIGHT  JOIN tableb B
on A.variety = B.variety ;

运行结果

3.7.全外链接

select * from tablea A LEFT  JOIN tableb B
on A.variety = B.variety Where B.variety is  null 
union
select * from tablea A RIGHT  JOIN tableb B
on A.variety = B.variety Where A.variety is  null

运行结果

             欢迎扫码关注个人微信公众号,或者微信公众号直接搜索Java传奇,不定时更新一些学习笔记!

                            

相关文章
相关标签/搜索