SQL Server 2005 - 查询帮助(SQL Server 2005 - Query Help)
我试图使用以下示例表编写查询:
公司
- CompanyID
- 名称
- 县
CompanyRelation
- ParentCompanyID
- ChildCompanyID
CompanySpecialty
- CompanyID
- 专业
使用
CompanyRelation
表是因为每个公司下面都有许多子公司,每个子公司可以有很多公司。理想情况下,我希望能找到的是某个县和/或某个专业的所有公司。 现在,通常情况下,我会加入
Company
到CompanySpecialty
并过滤这两个字段,但这里是棘手的:子公司没有分配给他们的县,所以如果我过滤县,所有的子公司将是排除。如果我将公司过滤到一个县,我想显示与该公司关联的所有子公司,无论子公司没有地址。
为了解决这个问题,我必须引入
CompanyRelation
表。示例数据:
CompanyID---Name---County 1-----------ABC----King 2-----------BCD----Pierce 3-----------DEF----NULL 4-----------EFG----NULL ParentCompanyID---ChildCompanyID 1-----------------1 1-----------------3 2-----------------2 2-----------------4 CompanyID---Specialty 1-----------Vehicles 2-----------Vehicles 3-----------Vehicles 4-----------Vehicles
使用这些数据,说我想找到金县所有处理车辆的公司。 在我的结果中,我希望看到公司1和公司3。
如何编写查询来完成此任务?
I am trying to write a query using the following example tables:
Company
- CompanyID
- Name
- County
CompanyRelation
- ParentCompanyID
- ChildCompanyID
CompanySpecialty
- CompanyID
- Specialty
The
CompanyRelation
table is used because each company can have many subcompanies under it and each subcompany can have many companies over it.Ideally, what I want to be able to find is all companies by a certain county and/or by a certain specialty. Now, normally, I would just join
Company
toCompanySpecialty
and filter on those two fields, but here is where it gets tricky: the subcompanies don't have counties assigned to them, so if I filter on the county, all the subcompanies will be excluded.If I filter the companies down to a county, I would like to display all subcompanies that are associated to that company, regardless of the fact that the subcompany does not have an address.
To get around this, I have to bring in the
CompanyRelation
table.Example data:
CompanyID---Name---County 1-----------ABC----King 2-----------BCD----Pierce 3-----------DEF----NULL 4-----------EFG----NULL ParentCompanyID---ChildCompanyID 1-----------------1 1-----------------3 2-----------------2 2-----------------4 CompanyID---Specialty 1-----------Vehicles 2-----------Vehicles 3-----------Vehicles 4-----------Vehicles
Using this data, say I want to find all companies in King county that deal with vehicles. In my results, I would expect to see Company 1 and Company 3.
How can I write a query to accomplish this?
原文:https://stackoverflow.com/questions/6710199
最满意答案
联合加入的两个查询将给出所需的结果:
( SELECT team AS team,SUM(goal) AS goals FROM table GROUP BY 1 LIMIT 4 ) UNION ( SELECT 'Other',SUM(other.goals) FROM ( SELECT team AS team,SUM(goal) AS goals FROM table GROUP BY 1 LIMIT 5,999999 ) other )
Two queries joined by a union will give the desired result:
( SELECT team AS team,SUM(goal) AS goals FROM table GROUP BY 1 LIMIT 4 ) UNION ( SELECT 'Other',SUM(other.goals) FROM ( SELECT team AS team,SUM(goal) AS goals FROM table GROUP BY 1 LIMIT 5,999999 ) other )
相关问答
更多-
Mysql中limit的用法[2023-10-05]
以上SELECT * FROM table LIMIT 15,15; 检索的为16-30行的数据 具体的limit的使用详解如下: SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset LIMIT 子句可以被用于强制 SELECT 语句返回指定的记录数。LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。初始记录行的偏移量是 0(而不是 ... -
mysql中limit的用法问题[2021-04-19]
返加所有记录就不用limit了,limit正是用来限制返回数据条数的。 直接用 select * from table 就可以了 -
mysql LIMIT使用问题[2024-03-16]
楼主 limit用法很简单 帮你举个例子吧 select * from table limit m,n 其中m是指记录开始的index,从0开始,表示第一条记录 n是指从第m+1条开始,取n条。 select * from tablename limit 2,4 即取出第3条至第6条,4条记录 如果你只有一条记录 但你要查询10条记录 这样就会重复取数了 如果楼主满意 请采纳 -
Mysql中limit的用法[2022-06-26]
limit是mysql的语法 select * from table limit m,n 其中m是指记录开始的index,从0开始,表示第一条记录 n是指从第m+1条开始,取n条。 select * from tablename limit 15,15 即取出第16条至第30条,15条记录 -
MySQL Select和LIMIT(MySQL Select and LIMIT)[2022-09-19]
SELECT id FROM ips WHERE ip IN (SELECT DISTINCT ip FROM ips ORDER BY id LIMIT 3) 由于MySQL 5.5还没有支持ANY内部的LIMIT ,这里有一个简单的解决方法: SELECT id FROM ips WHERE ip IN (SELECT * FROM (SELECT DISTINCT ip FROM ips ORDER BY id LIMIT 3) alias) SELECT id FROM ips WHERE ip ... -
MySQL:LIMIT 2,-1(MySQL: LIMIT 2,-1)[2022-12-23]
来自https://dev.mysql.com/doc/refman/5.0/en/select.html LIMIT子句可用于约束SELECT语句返回的行数。 LIMIT需要一个或两个数字参数,它们都必须是非负整数常量(使用预准备语句时除外)。 要从特定偏移量检索所有行直到结果集的末尾,可以使用一些大数字作为第二个参数。 此语句检索从第96行到最后一行的所有行: SELECT * FROM tbl LIMIT 95,18446744073709551615; 所以在你的情况下 $sql="SELECT ... -
这个怎么样(未经测试): SELECT * FROM table WHERE id = 0 AND NOT EXISTS (SELECT * FROM table WHERE active = 1) UNION SELECT * FROM table WHERE ACTIVE = 1; 这可能不是很有效,但它取决于记录的数量。 对于活动的索引可能有所帮助,以防万一。 它应该适合您的问题:如果有active = 1行,它们将从union的第二个查询返回,而第一个部分解析为空集(因为EXISTS子查询将失败) ...
-
如果您可以处理大约 30%,您可以简单地做: where brand = 'TBrand' and rand() <= 0.3 如果你想要一个更好的近似值30%,那么你可以这样做: SELECT x.* FROM (SELECT rdw.merchant_id, hash_id, transaction_ts, (@rn := @rn + 1) as rn FROM table_1 rdw JOIN table_2 bl O ...
-
我想如果你在MySQL客户端运行查询,限制为10,你会发现它实际上在结果集中返回了10行。 我怀疑@mcmodlist_tag_server中有多行,其server_id与@mcmodlist_servers中的行匹配。 当存在多个匹配行时,您将从@mcmodlist_servers获取“重复”行。 鉴于没有从@mcmodlist_tag_server表返回的列,并且这是一个OUTER连接,所以根本不清楚为什么这个表将被包含在查询中。 不,LEFT JOIN并不意味着你所说的意思。 问: 左外连接只有在匹配 ...
-
联合加入的两个查询将给出所需的结果: ( SELECT team AS team,SUM(goal) AS goals FROM table GROUP BY 1 LIMIT 4 ) UNION ( SELECT 'Other',SUM(other.goals) FROM ( SELECT team AS team,SUM(goal) AS goals FROM table GROUP BY 1 LIMIT 5,999999 ) other ) Two ...