从具有相同输入的多个表中进行选择 (SQL SERVER 2005)

发布于 2024-12-11 07:56:03 字数 589 浏览 0 评论 0原文

如何将几个结果集合并为一个。假设我有这五个 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 技术交流群。

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

发布评论

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

评论(1

寂寞清仓 2024-12-18 07:56:03

一种方法是使用公用表表达式,然后交叉连接它们

    with cte1 as ( 
    SELECT agentid, latitude, longitude, availability, updated
    FROM table1
    WHERE agentid=10)
    , cte2 as (

    SELECT email, name, phone, company
    FROM table2
    WHERE userid=10)

    , cte3 as (
    SELECT COUNT(*) AS pt1num
    FROM table3
    WHERE agentid=10 AND propertytype<6)

    , cte4 as (SELECT COUNT(*) AS pt2num
    FROM table3
    WHERE agentid=10 AND propertytype>6 AND propertytype<9)

    ,  cte5 as (
    SELECT COUNT(*) AS pt3num
    FROM table3
    WHERE agentid=10 AND propertytype>8)


SELECT [youfieldlist] 
FROM 
  cte1, cte2, cte3, cte4, cte5

另一种方法是使用内联视图

Select [your field list]
FROM (
SELECT agentid, latitude, longitude, availability, updated
FROM table1
WHERE agentid=10 ) t1,
(
SELECT email, name, phone, company
FROM table2
WHERE userid=10) t2, 
(
SELECT COUNT(*) AS pt1num
FROM table3
WHERE agentid=10 AND propertytype<6) t3,
(
SELECT COUNT(*) AS pt2num
FROM table3
WHERE agentid=10 AND propertytype>6 AND propertytype<9) t4,
(
SELECT COUNT(*) AS pt3num
FROM table3
WHERE agentid=10 AND propertytype>8) t5

只有当您知道每个选择只会返回一行时,这些才是真正合理的

您也可以使用局部变量来执行每个选择

DECLARE @agentid int, @latitude int , ... 
SELECT   @agentid = agentid,  @latitude = latitude, ...
        FROM table1
        WHERE agentid=10

DECLARE @pt1num int
 SELECT @pt1num  = COUNT(*)  
    FROM table3
    WHERE agentid=10 AND propertytype<6

,然后将它们选择出来

SELECT  @agentid agentid,  @latitude latitude, ... @pt1num... 

One way is to use Common table expressions and then cross join them

    with cte1 as ( 
    SELECT agentid, latitude, longitude, availability, updated
    FROM table1
    WHERE agentid=10)
    , cte2 as (

    SELECT email, name, phone, company
    FROM table2
    WHERE userid=10)

    , cte3 as (
    SELECT COUNT(*) AS pt1num
    FROM table3
    WHERE agentid=10 AND propertytype<6)

    , cte4 as (SELECT COUNT(*) AS pt2num
    FROM table3
    WHERE agentid=10 AND propertytype>6 AND propertytype<9)

    ,  cte5 as (
    SELECT COUNT(*) AS pt3num
    FROM table3
    WHERE agentid=10 AND propertytype>8)


SELECT [youfieldlist] 
FROM 
  cte1, cte2, cte3, cte4, cte5

Another way is to use inline views

Select [your field list]
FROM (
SELECT agentid, latitude, longitude, availability, updated
FROM table1
WHERE agentid=10 ) t1,
(
SELECT email, name, phone, company
FROM table2
WHERE userid=10) t2, 
(
SELECT COUNT(*) AS pt1num
FROM table3
WHERE agentid=10 AND propertytype<6) t3,
(
SELECT COUNT(*) AS pt2num
FROM table3
WHERE agentid=10 AND propertytype>6 AND propertytype<9) t4,
(
SELECT COUNT(*) AS pt3num
FROM table3
WHERE agentid=10 AND propertytype>8) t5

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

DECLARE @agentid int, @latitude int , ... 
SELECT   @agentid = agentid,  @latitude = latitude, ...
        FROM table1
        WHERE agentid=10

DECLARE @pt1num int
 SELECT @pt1num  = COUNT(*)  
    FROM table3
    WHERE agentid=10 AND propertytype<6

and then select them out

SELECT  @agentid agentid,  @latitude latitude, ... @pt1num... 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文