[mysql] MySQL how to join tables on two fields

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?

This question is related to mysql join

The answer is


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

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


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

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

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