This time it's about left join. These are table definitions:
CREATE TABLE `tb_workday` (
`id` int(11) DEFAULT NULL,
`wddate` date NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
tb_workday content sample:
+----+------------+
| id | wddate |
+----+------------+
| 1 | 2016-06-01 |
| 2 | 2016-06-02 |
| 3 | 2016-06-03 |
| 4 | 2016-06-04 |
| 5 | 2016-06-05 |
and tb_time
CREATE TABLE `tb_t` (
`TBegDate` date NOT NULL,
`TBegTime` time DEFAULT NULL,
PRIMARY KEY (`TBegDate`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
tb_time content sample:
+------------+----------+
| TBegDate | TBegTime |
+------------+----------+
| 2016-01-01 | 19:00:33 |
| 2016-01-02 | 22:23:33 |
| 2016-01-03 | 17:43:33 |
| 2016-01-04 | 00:02:33 |
| 2016-01-05 | 19:00:33 |
| 2016-01-06 | 06:28:33 |
| 2016-01-07 | 03:41:33 |
| 2016-01-08 | 11:24:33 |
This is the result I wanted. I want the table to show the tb_time result of wddate and wddate+1:
+----+------------+------------+----------+
| id | wddate | TBegDate | TBegTime |
+----+------------+------------+----------+
| 1 | 2016-06-01 | 2016-06-01 | 23:51:33 |
| 1 | 2016-06-01 | 2016-06-02 | 18:40:33 |
| 2 | 2016-06-02 | 2016-06-02 | 18:40:33 |
| 2 | 2016-06-02 | 2016-06-03 | 11:45:33 |
| 3 | 2016-06-03 | 2016-06-03 | 11:45:33 |
| 3 | 2016-06-03 | 2016-06-04 | 20:01:33 |
| 4 | 2016-06-04 | 2016-06-04 | 20:01:33 |
| 4 | 2016-06-04 | 2016-06-05 | 06:24:33 |
I am using this query
SELECT * FROM tb_workday
LEFT JOIN tb_time ON TBegDate IN (WDDate,DATE_ADD(WDDate,INTERVAL 1 DAY))
but EXPLAIN result of tb_time is 183 rows, which does not utilize the index at all.
Now I changed it to the exact date
SELECT * FROM tb_wd
LEFT JOIN tb_t ON TBegDate IN ("2016-01-01","2016-01-02")
This time the EXPLAIN result is 2 rows.
I wanted the former query to use the index. Please answer this without turning in into a UNION query. I asked this question before (How to make multiple LEFT JOINs with OR fully use a composite index? (part 2)) but because of my English I failed to express my problem I am having, so I made it more simple here. I want to use both the IN clause and the index.