MySQL MySQL如何连接两个字段上的表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/498197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 12:43:35  来源:igfitidea点击:

MySQL how to join tables on two fields

mysqljoin

提问by pedalpete

I have two tables with dateand idfields. I want to join on both fields. I tried

我有两个带有dateid字段的表。我想加入这两个领域。我试过

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?

这有效,但速度很慢。有一个更好的方法吗?

回答by womble

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

回答by Chad Birch

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

回答by Eugene Kaurov

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

也许你需要使用 INNEER JOIN 或 where t2.id is not null 如果你想要结果只匹配两个条件