欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mysql多表联合查询操作实例分析

程序员文章站 2023-11-29 08:06:04
本文实例讲述了mysql多表联合查询操作。分享给大家供大家参考,具体如下: mysql多表联合查询是mysql数据库的一种查询方式,下面就为您介绍mysql多表联合查询的...

本文实例讲述了mysql多表联合查询操作。分享给大家供大家参考,具体如下:

mysql多表联合查询是mysql数据库的一种查询方式,下面就为您介绍mysql多表联合查询的语法,供您参考学习之用。

mysql多表联合查询语法:

复制代码 代码如下:
select * from 插入表 left join 主表 on t1.lvid=t2.lv_id select * from mytable,title where 表名1.name=表名2.writer ;

mysql版本大于4.0,使用union进行查询,示例如下:

select `id`, `name`, `date`, '' as `type` from table_a where 条件语句……
 union
select `id`, `name`, `date`, '未完成' as `type` from table_b where 条件语句……
 order by `id` limit num;

mysql版本小于4.0,需要建立临时表,分为三步,示例如下:

第一步:建立临时表tmp_table_name并插入table_a中的相关记录

复制代码 代码如下:
$sql = "create temporary table tmp_table_name select `id`, `name`, `date`, '完成' as `type` from table_a where 条件语句……";

第二步:从table_b中取得相关记录插入临时表tmp_table_name中

复制代码 代码如下:
insert into tmp_table_name select `id`, `name`, `date2` as `date`, '未完成' as `type` from table_b where 条件语句……

第三步:从临时表tmp_table_name中取出记录

select * from tmp_table_name order by id desc

union和order by、limit区别分析

代码示例:

create table `test1` (
 `id` int(10) unsigned not null auto_increment,
 `name` varchar(20) not null,
 `desc` varchar(100) not null,
 primary key (`id`)
) engine=innodb default charset=utf8

1. 以下查询会报错误:[err] 1221 - incorrect usage of union and order by

代码示例:

select * from test1 where name like 'a%' order by name
union
select * from test1 where name like 'b%' order by name

修改为:

代码示例:

select * from test1 where name like 'a%'
union
select * from test1 where name like 'b%' order by name

说明,在union中,不用括号的情况下,只能用一个order by(思考:union两边的order by的列名不一样时,会出现什么样的结果?),这会对union后的结果集进行排序。

修改为:

代码示例:

(select * from test1 where name like 'a%' order by name)
union
(select * from test1 where name like 'b%' order by name)

也是可以的,这两个order by在union前进行。

2. 同样

代码示例:

select * from test1 where name like 'a%' limit 10
union
select * from test1 where name like 'b%' limit 20

相当于:

代码示例:

(select * from test1 where name like 'a%' limit 10)
union
(select * from test1 where name like 'b%') limit 20

即后一个limit作用于的是union后的结果集,而不是union后的select。
也可以用括号括起来,以得到预期的结果:

3. union和union all区别

union会过滤掉union两边的select结果集中的重复的行,而union all不会过滤掉重复的行。

代码示例:

(select * from test1 where name like 'a%' limit 10)
union
(select * from test1 where name like 'b%' limit 20)

下面试一个年龄段分析的复杂sql语句

mysql多表联合查询操作实例分析

(
 select
  '5~19' as `age`,
  sum(`impression`) as impression,
  sum(`click`) as click,
  sum(`cost`) as cost
 from
  `adgroup_age_report`
 where
  (
   (
    (`age` <= 19)
    and (`adgroup_id` = '61')
   )
   and (`date` >= '2015-11-22')
  )
 and (`date` <= '2017-02-20')
)
union
 (
  select
   '20~29' as `age`,
   sum(`impression`) as impression,
   sum(`click`) as click,
   sum(`cost`) as cost
  from
   `adgroup_age_report`
  where
   (
    (
     ((`age` <= 29) and(`age` >= 20))
     and (`adgroup_id` = '61')
    )
    and (`date` >= '2015-11-22')
   )
  and (`date` <= '2017-02-20')
 )
union
 (
  select
   '30~39' as `age`,
   sum(`impression`) as impression,
   sum(`click`) as click,
   sum(`cost`) as cost
  from
   `adgroup_age_report`
  where
   (
    (
     ((`age` <= 39) and(`age` >= 30))
     and (`adgroup_id` = '61')
    )
    and (`date` >= '2015-11-22')
   )
  and (`date` <= '2017-02-20')
 )
union
 (
  select
   '40~49' as `age`,
   sum(`impression`) as impression,
   sum(`click`) as click,
   sum(`cost`) as cost
  from
   `adgroup_age_report`
  where
   (
    (
     ((`age` <= 49) and(`age` >= 40))
     and (`adgroup_id` = '61')
    )
    and (`date` >= '2015-11-22')
   )
  and (`date` <= '2017-02-20')
 )
union
 (
  select
   '50~59' as `age`,
   sum(`impression`) as impression,
   sum(`click`) as click,
   sum(`cost`) as cost
  from
   `adgroup_age_report`
  where
   (
    (
     ((`age` <= 59) and(`age` >= 50))
     and (`adgroup_id` = '61')
    )
    and (`date` >= '2015-11-22')
   )
  and (`date` <= '2017-02-20')
 )

mysql多表联合查询操作实例分析

更多关于mysql相关内容感兴趣的读者可查看本站专题:《mysql查询技巧大全》、《mysql常用函数大汇总》、《mysql日志操作技巧大全》、《mysql事务操作技巧汇总》、《mysql存储过程技巧大全》及《mysql数据库锁相关技巧汇总

希望本文所述对大家mysql数据库计有所帮助。