sql - Can you use an AND statement in a JOIN in SQL
问题描述
Suppose you have two tables A and B and you are trying to write a JOIN query, is the following possible:
SELECT A.col1, B.col1
FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = 'hello')
Will this return a table of col1 from table A and col2 from table B where there is a match in the second column across the tables and the third column of table B is 'hello'?
I.e. it will only return rows that are matching in col2 and this is further reduced to the cases where col3 in table B is 'hello'?
解决方案
Yes. You can use:
- Below will Join the Records in B table (Col3='hello') with A:
SELECT A.col1, B.col1
FROM A JOIN B on (A.col2 = B.col2 AND B.col3 = 'hello')
- Below will Join all Records in B table with A, And performing where at Result of A and B:
SELECT A.col1, B.col1
FROM A JOIN B on A.col2 = B.col2
WHERE B.col3 = 'hello'
Both will give the same result when no other tables joined.
推荐阅读
- python - Python elementTree,如何在没有名称的多个VALUE中查找文本?
- kubernetes - 如何通过一个负载均衡器暴露多个 TCP 服务器
- docker - 在 Windows 上使用 WSL2 和 VS 代码容器进行 SSH 转发
- laravel - 使用 PHPUnit 运行 Laravel 测试,如果使用 QUEUE_CONNECTION = 'database',则不会创建作业
- matplotlib - 创建带有点的自定义 matplotlib 图例
- javascript - 将键和值添加到数组中最后一个重复的对象
- flutter - flutter windows打印插件给出CMake编译时错误
- azure - Microsoft.Identity.Web 库为带有 Azure Cosmos DB 的 ASP.NET Core 应用程序分布式令牌缓存。我们如何重用缓存的令牌?
- kubernetes - Istio 使用单个主机提交的多个端口号
- javascript - 这两个代码有什么区别。一个返回错误,第二个返回没有错误?