IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> MySQL 经典语句 -> 正文阅读

[大数据]MySQL 经典语句

MySQL 经典语句

一、查询语句

使用技术

  1. 多表查询,
  2. case when函数,
  3. if() 函数,
  4. unix_timestamp()函数,
  5. date_add()函数,
  6. 左连接

SQL语句

SELECT
  info.*,UNIX_TIMESTAMP(info.endDate) < UNIX_TIMESTAMP(NOW()) AS isWarning
FROM
  (
  SELECT
    jf.id AS facility_Id,
    jf.`name` AS facility_name,
    jft.name AS
        facility_class_name,
    jf.`type_id` AS facility_class_id,
    jf.`division_id`, 
    jf.`manage_unit_id`,
    jft.maintain_period,
    jft.is_regular_maintenance,
    jf.`recent_maintain_date`,
    jf.`default_maintain_date`,
    jf.address,
    jf.geo_location,
    CASE
      WHEN jft.`maintain_period` = 'WEEK' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 WEEK)
      WHEN jft.`maintain_period` = 'MONTH' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 MONTH)
      WHEN jft.`maintain_period` = 'QUARTER' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 3 MONTH)
      WHEN jft.`maintain_period` = 'HALF_YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 6 WEEK)
      WHEN jft.`maintain_period` = 'YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 YEAR)
    END AS endDate
    
  FROM
    `jcss_facility` jf
  LEFT JOIN `jcss_facility_type` jft ON
    jf.`type_id` = jft.`id`
  WHERE
    jft.`is_regular_maintenance` = TRUE
    AND jf.`deleted` = FALSE
    AND jft.`deleted` = FALSE) AS info

二、分页条件查询

使用技术

  1. 多表查询,
  2. case when函数,
  3. if() 函数,
  4. unix_timestamp()函数,
  5. date_add()函数,
  6. 左连接

mysql 语句

sql 语句一


SELECT
  info.*,UNIX_TIMESTAMP(info.endDate) < UNIX_TIMESTAMP(NOW()) AS isWarning
FROM
  (
  SELECT
    jf.id AS facility_Id,
    jf.`name` AS facility_name,
    jft.name AS
        facility_class_name,
    jf.`type_id` AS facility_class_id,
    jf.`division_id`, 
    jf.`manage_unit_id`,
    jft.maintain_period,
    jft.is_regular_maintenance,
    jf.`recent_maintain_date`,
    jf.`default_maintain_date`,
    jf.address,
    jf.geo_location,
    jf.deleted as facility_deleted,
    CASE
      WHEN jft.`maintain_period` = 'WEEK' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 WEEK)
      WHEN jft.`maintain_period` = 'MONTH' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 MONTH)
      WHEN jft.`maintain_period` = 'QUARTER' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 3 MONTH)
      WHEN jft.`maintain_period` = 'HALF_YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 6 WEEK)
      WHEN jft.`maintain_period` = 'YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 YEAR)
    END AS endDate
    
  FROM
    `jcss_facility` jf
  LEFT JOIN `jcss_facility_type` jft ON
    jf.`type_id` = jft.`id`
  WHERE
    jft.`is_regular_maintenance` = TRUE
    AND jf.`deleted` = FALSE
    AND jft.`deleted` = FALSE) AS info
    
    where info.endDate > '2020-04.-12' and info.endDate <'2023-01.-12'
    and UNIX_TIMESTAMP(info.endDate) < UNIX_TIMESTAMP(NOW()) =true 
    

sql 语句二


SELECT
  info.*,UNIX_TIMESTAMP(info.endDate) < UNIX_TIMESTAMP(NOW()) AS isWarning
FROM
  (
  SELECT
    jf.id AS facility_Id,
    jf.tenant_id,
    jf.`name` AS facility_name,
    jft.name AS
        facility_class_name,
    jf.`type_id` AS facility_class_id,
    jf.`division_id`, 
    jf.`manage_unit_id`,
    jft.maintain_period,
    jft.is_regular_maintenance,
    ifnull(jf.`recent_maintain_date`, jf.`default_maintain_date`) as recent_maintain_date,
    jf.`default_maintain_date`,
    jf.address,
    jf.geo_location,
    jf.deleted as facility_deleted,
    CASE
      WHEN jft.`maintain_period` = 'WEEK' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 WEEK)
      WHEN jft.`maintain_period` = 'MONTH' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 MONTH)
      WHEN jft.`maintain_period` = 'QUARTER' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 3 MONTH)
      WHEN jft.`maintain_period` = 'HALF_YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 6 WEEK)
      WHEN jft.`maintain_period` = 'YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL, jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 YEAR)
    END AS endDate
    
  FROM
    `jcss_facility` jf
  LEFT JOIN `jcss_facility_type` jft ON
    jf.`type_id` = jft.`id`
  WHERE
    jft.`is_regular_maintenance` = TRUE
    AND jf.`deleted` = FALSE
    AND jft.`deleted` = FALSE) AS info
    
    where info.endDate > '2020-04.-12' and info.endDate <'2023-01.-12'
    
   -- and UNIX_TIMESTAMP(info.endDate) < UNIX_TIMESTAMP(NOW()) =true 
    order by info.default_maintain_date desc 
    

xml 文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vortex.cloud.jcss.reborn.dao.FacilityMaintenanceMapper">
    <select id="getFacilityMaintenancePage" resultType="com.vortex.cloud.jcss.reborn.vo.FacilityMaintenanceVO"
            parameterType="com.vortex.cloud.jcss.reborn.dto.query.FacilityMaintenanceQueryDTO">
        SELECT
        info.*,UNIX_TIMESTAMP(info.endDate) &lt; UNIX_TIMESTAMP(NOW()) AS isWarning
        FROM
        (
        SELECT
        jf.id AS facility_Id,
        jf.tenant_id,
        jf.`name` AS facility_name,
        jft.name AS
        facility_class_name,
        jf.`type_id` AS facility_class_id,
        jf.`division_id`,
        jf.`manage_unit_id`,
        jft.maintain_period,
        jft.is_regular_maintenance,
        ifnull(jf.`recent_maintain_date`, jf.`default_maintain_date`) as recent_maintain_date,
        jf.`default_maintain_date`,
        jf.address,
        jf.geo_location,
        jf.deleted as facility_deleted,
        CASE
        WHEN jft.`maintain_period` = 'WEEK' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 WEEK)
        WHEN jft.`maintain_period` = 'MONTH' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 MONTH)
        WHEN jft.`maintain_period` = 'QUARTER' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 3 MONTH)
        WHEN jft.`maintain_period` = 'HALF_YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 6 WEEK)
        WHEN jft.`maintain_period` = 'YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 YEAR)
        END AS endDate

        FROM
        `jcss_facility` jf
        LEFT JOIN `jcss_facility_type` jft ON
        jf.`type_id` = jft.`id`
        WHERE
        jft.`is_regular_maintenance` = TRUE
        AND jf.`deleted` = FALSE
        AND jft.`deleted` = FALSE) AS info
        <where>
            info.facility_deleted = false
            <if test="queryDto.facilityClassId !=null and queryDto.facilityClassId !=''">
                and info.facility_class_id =#{queryDto.facilityClassId}
            </if>
            <if test="queryDto.facilityName !=null and queryDto.facilityName !=''">
                and info.facility_name LIKE concat("%",#{queryDto.facilityName},"%")
            </if>
            <if test="queryDto.divisionId !=null and queryDto.divisionId !=''">
                and info.division_id =#{queryDto.divisionId}
            </if>
            <if test="queryDto.manageUnitId !=null and queryDto.manageUnitId !=''">
                and info.manage_unit_id =#{queryDto.manageUnitId}
            </if>
            <if test="queryDto.startDate !=null and queryDto.startDate !=''">
                and info.endDate &gt;= #{queryDto.startDate}
            </if>
            <if test="queryDto.endDate !=null and queryDto.endDate !=''">
                and info.endDate &lt;= #{queryDto.endDate}
            </if>
            <if test="queryDto.isWarning  !=null and queryDto.isWarning">
                and UNIX_TIMESTAMP(info.endDate) &lt; UNIX_TIMESTAMP(NOW()) =true
            </if>
            <if test="queryDto.isWarning  !=null and !queryDto.isWarning">
                and UNIX_TIMESTAMP(info.endDate) &lt; UNIX_TIMESTAMP(NOW()) =false
            </if>
        </where>
    </select>

    <select id="getFacilityMaintenanceList" resultType="com.vortex.cloud.jcss.reborn.vo.FacilityMaintenanceVO">
        SELECT
        info.*,UNIX_TIMESTAMP(info.endDate) &lt; UNIX_TIMESTAMP(NOW()) AS isWarning
        FROM
        (
        SELECT
        jf.id AS facility_Id,
        jf.tenant_id,
        jf.`name` AS facility_name,
        jft.name AS
        facility_class_name,
        jf.`type_id` AS facility_class_id,
        jf.`division_id`,
        jf.`manage_unit_id`,
        jft.maintain_period,
        jft.is_regular_maintenance,
        ifnull(jf.`recent_maintain_date`, jf.`default_maintain_date`) as recent_maintain_date,
        jf.`default_maintain_date`,
        jf.address,
        jf.geo_location,
        jf.deleted as facility_deleted,
        CASE
        WHEN jft.`maintain_period` = 'WEEK' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 WEEK)
        WHEN jft.`maintain_period` = 'MONTH' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 MONTH)
        WHEN jft.`maintain_period` = 'QUARTER' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 3 MONTH)
        WHEN jft.`maintain_period` = 'HALF_YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 6 WEEK)
        WHEN jft.`maintain_period` = 'YEAR' THEN DATE_ADD(IF(jf.`recent_maintain_date` IS NULL,
        jf.`default_maintain_date`, jf.`recent_maintain_date`), INTERVAL 1 YEAR)
        END AS endDate

        FROM
        `jcss_facility` jf
        LEFT JOIN `jcss_facility_type` jft ON
        jf.`type_id` = jft.`id`
        WHERE
        jft.`is_regular_maintenance` = TRUE
        AND jf.`deleted` = FALSE
        AND jft.`deleted` = FALSE) AS info
        <where>
            info.facility_deleted = false
            <if test="ew.sqlSegment !=null and ew.sqlSegment !=''">
                and ${ew.sqlSegment}
            </if>
        </where>
    </select>
</mapper>

mapper 类

  /**
     * 分页查询
     *
     * @param page
     * @param queryDto
     * @return
     */
    IPage<FacilityMaintenanceVO> getFacilityMaintenanceList(Page<FacilityMaintenanceVO> page, @Param("queryDto") FacilityMaintenanceQueryDTO queryDto);
     /**
     * 获取所有预警设施数据
     *
     * @param ew
     * @return
     */
    List<FacilityMaintenanceVO> getFacilityMaintenanceList(@Param("ew") QueryWrapper<FacilityMaintenanceQueryDTO> ew);

关注林哥,持续更新哦!!!★,°:.☆( ̄▽ ̄)/$:.°★ 。

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-03-16 22:27:35  更:2022-03-16 22:28:58 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 7:26:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码