sql – 如何将具有公共列(A,B)和(A,C)的2个查询转换为一个(A,B,C)?
发布时间:2020-05-24 19:40:56 所属栏目:MsSql 来源:互联网
导读:我目前有2个返回的查询 PRODUCER FirstQueryColumn ------------------------------ ---------------------- aaaaaaaaaaaa 1 bbbbbbbbbbb
|
我目前有2个返回的查询 PRODUCER FirstQueryColumn ------------------------------ ---------------------- aaaaaaaaaaaa 1 bbbbbbbbbbb 1 PRODUCER SecondQueryColumn ------------------------------ ---------------------- aaaaaaaaaaaa 2 bbbbbbbbbbb 1 我想知道的是我应该如何制作它以便我可以在一个查询中获得相同的数据,也就是说,我想要的东西会产生(Producer,FirstQueryColumn,SecondQueryColumn). 我怎样才能做到这一点? 这是我目前的查询: select Producers.name Prod,count(Animals.idanimal) AnimalsBought from AnimalsBought,Animals,Producers where (AnimalsBought.idanimal = Animals.idanimal) and (Animals.owner = Producers.nif) group by Producers.name; select Producers.name Prod,count(Animals.idanimal) AnimalsExploration from AnimalsExploration,Producers where (AnimalsExploration.idanimal = Animals.idanimal) and (Animals.owner = Producers.nif) group by Producers.name; 如您所见,对于这种情况,连接不会做太多: select Producers.name Prod,count(AnimalsBought.idanimal) AnimalsBought,count(AnimalsExploration.idanimal) AnimalsExploration from Producers,AnimalsBought,AnimalsExploration where (AnimalsExploration.idanimal = Animals.idanimal) and (Animals.owner = Producers.nif) group by Producers.name; 或者我做错了什么? 解决方法我认为animals.idanimal是主键.如果是这样,可以使用左外连接编写查询并在目标列上计数以切断NULL.select producers.name prod,count(animalsbought.idanimal) animalsbought,count(animalsexploration.idanimal) animalsexploration from producers join animals on animals.owner = producers.nif left join animalsbought on animalsbought.idanimal = animals.idanimal left join animalsexploration on animalsexploration.idanimal = animals.idanimal group by producers.name; (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- sql-server – 如何判断我的SQL数据库有哪些恢复模型?
- SQL Server 在数据库‘master’中拒绝CREATE DATABASE权限问
- sql-server – 什么时候创建STATISTICS而不是创建索引更好?
- tridion – 多个部署者单个Content Delivery数据库(Broker
- sql – 在Oracle中regexp_replace vs translate的性能?
- sql-server – 将数据库图添加到源代码管理?
- SQL标识(1,1)从0开始
- Mysql limit 优化,百万至千万级快速分页 复合索引的引用并
- sql-server – Invoke-SqlCmd不返回长字符串?
- sql – 如何查找无向图的所有连接子图
