mysql - 选择日期范围之间的表并集

标签 mysql mysql-workbench

我尝试选择多个表,其中结束日期或开始日期不在范围内。

SELECT *
FROM excel.construction
GROUP BY startDate,
         endDate
UNION ALL

SELECT *
FROM excel.misc
GROUP BY startDate,
         endDate
UNION ALL

SELECT *
FROM excel.spawar
GROUP BY startDate,
         endDate
WHERE construction.startDate BETWEEN '2012-01-01' AND '2012-01-10'
  OR construction.endDate BETWEEN '2012-01-01' AND '2012-01-10'
  OR misc.startDate BETWEEN '2012-01-01' AND '2012-01-10'
  OR misc.endDate BETWEEN '2012-01-01' AND '2012-01-10';

为什么这不起作用/有更好的方法吗?

最佳答案

一些事情

这 3 个选择必须具有相同的列数和类型

SELECT * FROM excel.construction  
SELECT * FROM excel.misc 
SELECT * FROM excel.spawar 

where 条件必须与正确的查询相关

SELECT * 
    FROM excel.construction 
    where construction.startDate  BETWEEN '2012-01-01' AND '2012-01-10' 
    OR construction.endDate BETWEEN '2012-01-01' AND '2012-01-10'
    group by startDate,endDate
UNION ALL 
select * 
    FROM excel.misc 
    where misc.startDate BETWEEN '2012-01-01' AND '2012-01-10' OR 
         misc.endDate BETWEEN '2012-01-01' AND '2012-01-10';m       
    group by  startDate, endDate 
UNION ALL 
select * 
    from excel.spawar 
group by startDate, endDate 

关于mysql - 选择日期范围之间的表并集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38061163/

相关文章:

python - MySQL不存储 float

mysql - Excel 到 SQL - 非常大的文件

mysql - 如何自动递增唯一值

mysql - MySQL 中的数据归档

mysql - 如何将 IN 条件转换为 INNER JOIN 条件 - 连接速度较慢

mysql - 函数计数数据结果以逗号分隔

mysql - 在 MySQL 中将存储的 md5 字符串转换为十进制值

mysql - 创建存储过程 - SQL

mysql - MySQL如何检测中文字符?

MYSQL 有计数或有计数问题