MySQL 用 IF 语句连接重复项

标签 mysql join

mysql> describe holidays;
+---------+-------------+
| Field   | Type        |
+---------+-------------+
| hid     | int(11)     |
| name    | varchar(32) |
| date    | date        |
| enabled | int(1)      |
+---------+-------------+

mysql> 

mysql> describe locations;
+----------+--------------+
| Field    | Type         |
+----------+--------------+
| lid      | int(11)      |
| code     | char(2)      |
| location | varchar(255) |
| enabled  | int(1)       |
+----------+--------------+

mysql> describe holidays_locations;
+-------+---------+
| Field | Type    |
+-------+---------+
| hid   | int(11) |
| lid   | int(11) |
+-------+---------+

mysql> SELECT DISTINCT timesheet.tid, timesheet.uid, timesheet.date, timesheet.location,
IF (timesheet.date = holidays.date AND timesheet.location = holidays.lid, 1, 0) AS is_holiday
FROM (`timesheet`)
LEFT JOIN (SELECT holidays.name AS holiday, locations.location as location, holidays.date AS date, locations.lid AS lid
            FROM holidays_locations
            INNER JOIN holidays ON holidays_locations.hid = holidays.hid
            INNER JOIN locations ON holidays_locations.lid = locations.lid) AS holidays ON holidays.date = timesheet.date
WHERE `uid` =  '12'
AND `timesheet`.`date` >= '2012-05-16'
AND `timesheet`.`date` <= '2012-05-31'
ORDER BY date;

+------+-----+------------+----------+------------+
| tid  | uid | date       | location | is_holiday |
+------+-----+------------+----------+------------+
| 2962 |  12 | 2012-05-18 | 5        |          0 |
| 3163 |  12 | 2012-05-21 | 9        |          1 |
| 3163 |  12 | 2012-05-21 | 9        |          0 |
| 3162 |  12 | 2012-05-21 | 5        |          0 |
+------+-----+------------+----------+------------+

我的问题是,如何在没有重复的“3163”条目的情况下执行此查询?以下是我所期待的。

+------+-----+------------+----------+------------+
| tid  | uid | date       | location | is_holiday |
+------+-----+------------+----------+------------+
| 2962 |  12 | 2012-05-18 | 5        |          0 |
| 3163 |  12 | 2012-05-21 | 9        |          1 |
| 3162 |  12 | 2012-05-21 | 5        |          0 |
+------+-----+------------+----------+------------+

最佳答案

您可以消除 is_holiday = 0MAX()聚合,按 SELECT 中的所有其他列分组列表。

SELECT 
  DISTINCT timesheet.tid, 
  timesheet.uid, 
  timesheet.date, 
  timesheet.location,
  /* MAX aggregate will take only the 1, not 0 */
  MAX(IF (timesheet.date = holidays.date AND timesheet.location = holidays.lid, 1, 0)) AS is_holiday
FROM (`timesheet`)
LEFT JOIN (SELECT holidays.name AS holiday, locations.location as location, holidays.date AS date, locations.lid AS lid
            FROM holidays_locations
            INNER JOIN holidays ON holidays_locations.hid = holidays.hid
            INNER JOIN locations ON holidays_locations.lid = locations.lid) AS holidays ON holidays.date = timesheet.date
WHERE `uid` =  '12'
AND `timesheet`.`date` >= '2012-05-16'
AND `timesheet`.`date` <= '2012-05-31'
/* And GROUP BY all other selected columns */
GROUP BY 
  timesheet.tid, 
  timesheet.uid, 
  timesheet.date, 
  timesheet.location
ORDER BY date;

关于MySQL 用 IF 语句连接重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11676884/

相关文章:

php - 如何编写此 SQL 语句来获取广告并发布? (PHP/MySQL)

mysql - 外部和内部 SQL 连接

mysql - 在 MySQL 中使用 REGEXP 在逗号分隔列表中查找特定数字

sql - 如何进行分组的多表SQL连接

MySQL 连接来自另一个表的计数匹配记录,但我也需要零值

mysql - 带有文本的大型表上 SQL 连接的过滤顺序

sql - MySQL:创建访问系统的最佳方式是什么?

mysql - brew 启动旧版本的 mysql 服务,但如何连接到它?

mongodb - 为什么 Drill join 查询没有完全针对 Mongodb 进行优化?

sql - 如何将此 LEFT JOIN 返回的行数限制为一?