SQL 连接表有两个“ON”关键词?

发布于 2024-09-10 03:27:44 字数 366 浏览 3 评论 0原文

我有两个表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

罪#恶を代价 2024-09-17 03:27:44

您的查询是正确的,无需添加额外的 On 关键字
AND 关键字在两个不同的列上进行连接工作。只要加上括号就可以正确理解。

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;

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.

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;
我一直都在从未离去 2024-09-17 03:27:44

你的方式应该可以正常工作。

在 MySQL 中,您可以使用 AND 设置多个连接约束。

The way you put it should work fine.

In MySQL you can set multiple join constraints by using AND.

盛夏已如深秋| 2024-09-17 03:27:44

Sql 服务器允许这样做。我可以谈谈其他实现。

Sql server allows this. I can speak for other implementations.

月亮邮递员 2024-09-17 03:27:44

它工作正常,但使用别名而不是长表名

select * from tbl_a a 
         join tbl_b b on a.a_c_id=b.b_c_id AND a.a_d_id=b.b_d_id 
         where a.id>15;

it works fine but use alias instead of long tablename

select * from tbl_a a 
         join tbl_b b on a.a_c_id=b.b_c_id AND a.a_d_id=b.b_d_id 
         where a.id>15;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文