MySQL如何根据不同条件联查不同表的数据if/case

作者:Coo~ 时间:2024-01-21 14:20:10 

MySQL根据不同条件联查不同表的数据

项目开发中遇到类似的需求。Mybatis 中的< if >标签只能判断where部分,不能满足要求。

在网上查解决方法,好像并没有可以完美解决问题的方案,if和case可以从某一种角度实现效果。

if

MySQL中if的用法

IF(expr1,expr2,expr3)

类似三元运算符,判断expr1,如果 expr1 是TRUE,则该语句的返回值为expr2; 否则返回值为 expr3。

SELECT product_id,
IF(product_type_id=1, (SELECT COUNT(*) FROM `device` WHERE product_id = product.product_id),
 (SELECT COUNT(*) FROM `monitor`WHERE product_id = product.product_id)) as device_num
FROM `product`
WHERE product_id in (1,25)

查product_id为(1,25)两产品的设备数。

product表中字段product_type_id表示产品类型。

如果是1就查device表,否则就查monitor表。

MySQL如何根据不同条件联查不同表的数据if/case

case

多于两种情况时 

case的用法

CASE expr1 WHEN a THEN A WHEN … THEN … ELSE … END

判断expr1,满足不同条件时执行不同的语句。

注:最后以END结尾

SELECT product_id,
CASE product_type_id
WHEN 1 THEN (SELECT COUNT(*) FROM `device` WHERE product_id = product.product_id)
WHEN 2 THEN (SELECT COUNT(*) FROM `monitor`WHERE product_id = product.product_id)
  ELSE (SELECT COUNT(*) FROM `other_device`WHERE product_id = product.product_id) END as device_num
FROM `product`
WHERE product_id in (1,11,25)

MySQL如何根据不同条件联查不同表的数据if/case

注意

这样能勉强实现效果,但并不是想象的那种。

话说,这种情况是不是应该先查出共同的部分,再在service层进行判断,执行不同的语句。。

MySQL两表联查,根据不同条件获得不同数据

场景:

查找某张表中某一列的所有符合某种条件的条目的累加和,且该表和另一张表相关联

查询语句

select DISTINCT ??
ifnull((select sum(‘列名') from a, b where a.id = b.id and a.condition=condition1 and b.condition = condition2),0) as '条件1下的数据' ,
ifnull((select sum(‘列名') from a, b where a.id = b.id and a.condition=condition3 and b.condition = condition4),0) as '条件2下的数据'
from a, b where a.id = b.id?

缺点:数据量一太大就会be崩溃

优化后语句

select ‘别名1', ‘别名2'
from?
(select IFNULL(sum(‘列名'),0) ‘别名1' from a, b where a.id = b.id and a.condition=condition1 and b.condition = condition2),0) t1
left join?
(select IFNULL(sum(‘列名'),0) ‘别名2' from a, b where a.id = b.id and a.condition=condition3 and b.condition = condition4) t2 on 1=1

left join查询比select嵌套查询效率高的原因:执行子查询时,MYSQL需要创建临时表,查询完毕后再删除这些临时表,所以,子查询的速度会受到一定的影响,这里多了一个创建和销毁临时表的过程。

来源:https://blog.csdn.net/qq_28869233/article/details/103305802

标签:MySQL,联查,不同表,数据,if,case
0
投稿

猜你喜欢

  • 一个ASP记录集分页显示的例子

    2007-09-14 10:57:00
  • Python 数据可视化pyecharts的使用详解

    2021-07-07 20:29:55
  • Python实现判断一个整数是否为回文数算法示例

    2022-05-13 12:01:07
  • Django缓存Cache使用详解

    2023-09-06 02:16:33
  • 快速认识CSS中的overflow属性

    2009-05-29 16:36:00
  • Python元组拆包和具名元组解析实例详解

    2021-10-17 05:26:58
  • js实现带有介绍的Select列表菜单实例

    2024-04-19 10:26:22
  • python面向对象 反射原理解析

    2021-05-14 08:56:32
  • 修改 CentOS 6.x 上默认Python的方法

    2023-08-07 10:02:59
  • python实现简单的超市商品销售管理系统

    2021-08-24 00:07:32
  • OpenCV imread读取图片失败的问题及解决

    2021-10-20 06:44:00
  • Python3.5 Pandas模块缺失值处理和层次索引实例详解

    2021-05-20 00:35:50
  • python 读取Linux服务器上的文件方法

    2023-03-31 23:54:19
  • python lxml中etree的简单应用

    2022-01-11 09:34:15
  • SQL Server 触发器实例详解

    2024-01-28 05:40:46
  • python调用摄像头拍摄数据集

    2021-08-12 10:25:22
  • TensorFlow的权值更新方法

    2022-12-24 21:41:08
  • golang如何修改json文件内容的方法示例

    2024-04-26 17:32:44
  • 如何使用Python 打印各种三角形

    2023-10-27 00:12:48
  • ORACLE 数据库RMAN备份恢复

    2009-04-24 12:23:00
  • asp之家 网络编程 m.aspxhome.com