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?
If you'd like to get data stored in tables joined by a compound key that's a primary key in one table and a foreign key in another table, simply use a join condition on multiple columns. In one joined table (in our example, enrollment ), we have a primary key built from two columns ( student_id and course_code ).
Yes: You can use Inner Join to join on multiple columns.
JOIN t2 ON t1.id=t2.id AND t1.date=t2.date
JOIN t2 ON (t2.id = t1.id AND t2.date = t1.date)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With