从具有相同输入的多个表中进行选择 (SQL SERVER 2005)
如何将几个结果集合并为一个。假设我有这五个 sql 选择,它们都采用相同的“简单”输入 (10):
SELECT agentid, latitude, longitude, availability, updated
FROM table1
WHERE agentid=10
SELECT email, name, phone, company
FROM table2
WHERE userid=10
SELECT COUNT(*) AS pt1num
FROM table3
WHERE agentid=10 AND propertytype<6
SELECT COUNT(*) AS pt2num
FROM table3
WHERE agentid=10 AND propertytype>6 AND propertytype<9
SELECT COUNT(*) AS pt3num
FROM table3
WHERE agentid=10 AND propertytype>8
如何 UNION 或 JOIN 或子查询,以便获得包含所有列的行; agentid、纬度、经度、可用性、更新、电子邮件、姓名、电话、公司、pt1num、pt2num、pt3num?
How do I combine a couple of resultsets into one. Say I have these five sql selects, which all take the same 'simple' input (10):
SELECT agentid, latitude, longitude, availability, updated
FROM table1
WHERE agentid=10
SELECT email, name, phone, company
FROM table2
WHERE userid=10
SELECT COUNT(*) AS pt1num
FROM table3
WHERE agentid=10 AND propertytype<6
SELECT COUNT(*) AS pt2num
FROM table3
WHERE agentid=10 AND propertytype>6 AND propertytype<9
SELECT COUNT(*) AS pt3num
FROM table3
WHERE agentid=10 AND propertytype>8
How do I UNION or JOIN or sub-query, so that I get a row with all the columns; agentid, latitude, longitude, availability, updated, email, name, phone, company, pt1num, pt2num, pt3num?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是使用公用表表达式,然后交叉连接它们
另一种方法是使用内联视图
只有当您知道每个选择只会返回一行时,这些才是真正合理的
您也可以使用局部变量来执行每个选择
,然后将它们选择出来
One way is to use Common table expressions and then cross join them
Another way is to use inline views
These are only really reasonable if you know each select is only going to return one row
You could also use local variables to do each select
and then select them out