MySQL how to join tables on two fields

MysqlJoin

Mysql Problem Overview


I have two tables with date and id fields. I want to join on both fields. I tried

JOIN t2 ON CONCAT(t1.id, t1.date)=CONCAT(t2.id, t2.date)

that works, but it is very slow. is there a better way to do this?

Mysql Solutions


Solution 1 - Mysql

JOIN t2 ON t1.id=t2.id AND t1.date=t2.date

Solution 2 - Mysql

JOIN t2 ON (t2.id = t1.id AND t2.date = t1.date)

Solution 3 - Mysql

SELECT * 
FROM t1
JOIN t2 USING (id, date)

perhaps you'll need to use INNEER JOIN or where t2.id is not null if you want results only matching both conditions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpedalpeteView Question on Stackoverflow
Solution 1 - MysqlwombleView Answer on Stackoverflow
Solution 2 - MysqlChad BirchView Answer on Stackoverflow
Solution 3 - MysqlEugene KaurovView Answer on Stackoverflow