SQL 连接表有两个“ON”关键词?
我有两个表 tbl_a 和 tbl_b ,它们的字段是
tbl_a
-----
a_id a_name a_surname a_c_id a_d_id
tbl_b
-----
b_id b_name b_phone b_c_id b_d_id
我想像这样连接这两个表:
select *from tbl_a join tbl_b on tbl_a.a_c_id=tbl_b.b_c_id AND tbl_a.a_d_id=tbl_b.b_d_id where tbl_a.id>15;
如您所见,我想对 ON 关键字使用两个键,有什么办法可以做到这一点吗? 感谢您提前...
I have two tables tbl_a and tbl_b and their fields are
tbl_a
-----
a_id a_name a_surname a_c_id a_d_id
tbl_b
-----
b_id b_name b_phone b_c_id b_d_id
I want to join these two table like this:
select *from tbl_a join tbl_b on tbl_a.a_c_id=tbl_b.b_c_id AND tbl_a.a_d_id=tbl_b.b_d_id where tbl_a.id>15;
As you can see i want to use two keys for ON keyword,is there anyway to do this?
Thanks for advance...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的查询是正确的,无需添加额外的 On 关键字
AND 关键字在两个不同的列上进行连接工作。只要加上括号就可以正确理解。
Your query is right there is no need for putting extra On keyword
AND keyword do work of join in on two different columns. Just put the bracket to understand properly.
你的方式应该可以正常工作。
在 MySQL 中,您可以使用 AND 设置多个连接约束。
The way you put it should work fine.
In MySQL you can set multiple join constraints by using AND.
Sql 服务器允许这样做。我可以谈谈其他实现。
Sql server allows this. I can speak for other implementations.
它工作正常,但使用别名而不是长表名
it works fine but use alias instead of long tablename