129

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?

3 Answers 3

224
JOIN t2 ON t1.id=t2.id AND t1.date=t2.date
0
47
JOIN t2 ON (t2.id = t1.id AND t2.date = t1.date)
33
SELECT * 
FROM t1
JOIN t2 USING (id, date)

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

1
  • 2
    Basically, slowness is because concrete calculates new values that has no indexes so direct conditions should be faster. if even this query would be slow again, check is indexes exists, and sometimes creation one index for 2 fields also makes sense. Commented May 14, 2013 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.