数据库 
首页 > 数据库 > 浏览文章

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

(编辑:jimmy 日期: 2024/11/16 浏览:3 次 )

本文实例讲述了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数据库计有所帮助。

上一篇:MySql删除表中一行的实操方法
下一篇:mysql 5.7.21 解压版安装配置图文教程
一句话新闻
微软与英特尔等合作伙伴联合定义“AI PC”:键盘需配有Copilot物理按键
几个月来,英特尔、微软、AMD和其它厂商都在共同推动“AI PC”的想法,朝着更多的AI功能迈进。在近日,英特尔在台北举行的开发者活动中,也宣布了关于AI PC加速计划、新的PC开发者计划和独立硬件供应商计划。
在此次发布会上,英特尔还发布了全新的全新的酷睿Ultra Meteor Lake NUC开发套件,以及联合微软等合作伙伴联合定义“AI PC”的定义标准。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 站点导航 SiteMap