Sql语句预告片

时间:2009-08-04 15:47:36

标签: sql-server stored-procedures

好的,所以这就是我想要实现的是拥有两个SELECT语句,然后加入每个语句的结果,所以我有这样的东西

SELECT table.ID, tst.Value 
FROM blah AS table JOIN results AS tst ON tst.RUNID = table.RUNID
WHERE table.RUNID IN
(
  ...// nothing important but in the end i get my tst.Value  
)

第二个陈述几乎相同

SELECT table.ID, tst2.Value 
FROM blah AS table JOIN results AS tst2 ON tst2.RUNID = table.RUNID
WHERE table.RUNID IN
(
  ...// nothing important but in the end i get my tst2.Value differently
)

我需要以

的格式组合这两个结果
SELECT table.ID, tst.Value, tst2.Value
...... // Somehow using those two statements

所以任何能说流利的SQL语言的人都可以告诉我该怎么做或者我应该使用什么语句...加入声音就像一个好的起点但他们使用表格。我想我可以使用SELECT中的CREATE TABLE并加入,但正如我所说,我不熟悉SQL,所以想知道这是好主意还是有更好的想法......

谢谢:)

7 个答案:

答案 0 :(得分:3)

最简单的方法(根据您的描述)将IN子句移动到您的连接条件中,并在一个查询中执行此操作。像这样:

select
    table.ID,
    tst.Value as Value1,
    txt2.Value as Value2

from blah table

left join results tst on tst.RUNID = table.RUNID and tst.RUNID in (...first conditions...)
left join results tst2 on tst.RUNID = table.RUNID and tst2.RUNID in (...second conditions...)

这个结构对我来说有点荒谬(因为你每次都会以不同的方式过滤根表中的值)。您可以发布实际结构和SQL供我们查看吗?这可能会更清楚。

答案 1 :(得分:1)

天真的方法,假设table.ID与两个value字段相关:

SELECT first_subquery.ID, first_subquery.Value, second_subquery.Value
FROM (
  SELECT table.ID, tst.Value 
  FROM blah AS table JOIN results AS tst ON tst.RUNID = table.RUNID
  WHERE table.RUNID IN
  (
    ...// nothing important but in the end i get my tst.Value  
  )
) first_subquery
INNER JOIN (
  SELECT table.ID, tst2.Value 
  FROM blah AS table JOIN results AS tst2 ON tst2.RUNID = table.RUNID
  WHERE table.RUNID IN
  (
    ...// nothing important but in the end i get my tst2.Value differently
  )
) second_subquery
ON first_subquery.ID = second_subquery.ID

此查询基于您的两个联合。

答案 2 :(得分:0)

这应该适合你:

SELECT table.ID, tst.Value, tst2.Value 
FROM blah AS table 
Inner JOIN results AS tst ON tst.RUNID = table.RUNID
Inner JOIN results AS tst2 ON tst2.RUNID = table.RUNID
WHERE table.RUNID IN
(  
  ...// nothing important but in the end i get my tst2.Value differently
)

答案 3 :(得分:0)

可以在查询中使用多个JOIN。这应该可以解决你的问题。

答案 4 :(得分:0)

尝试类似:

SELECT table.ID, tst.Value, dt.Value
    FROM blah AS table 
        JOIN results AS tst ON tst.RUNID = table.RUNID
        left outer join (
                         SELECT table.ID, tst2.Value
                             FROM blah AS table 
                                 JOIN results AS tst2 ON tst2.RUNID = table.RUNID
                             WHERE table.RUNID IN(  ...// nothing important but in the end i get my tst2.Value differently)
                        ) dt ON table.id=dt.ID
    WHERE table.RUNID IN(  ...// nothing important but in the end i get my tst.Value  )

答案 5 :(得分:-1)

试试这个......

SELECT T1.ID, R1.Value,
       T2.RunId, R2.Value
FROM (blah AS T1 JOIN results AS R1
         ON R1.RUNID = table.RUNID
           And T1.RUNID IN ( /... / ))
    Cross Join (blah AS T2  JOIN results AS R2 
         ON R.RUNID = T2.RUNID
           And T2.RunId In (/... /))

答案 6 :(得分:-1)

试试这个:

select a.ID, a.Value, b.Value from 
(
  SELECT table.ID, tst.Value 
  FROM blah AS table JOIN results AS tst ON tst.RUNID = table.RUNID
  WHERE table.RUNID IN
  (
    ...// nothing important but in the end i get my tst.Value  
  ) 
 ) a, 
(
  SELECT table.ID, tst2.Value 
  FROM blah AS table JOIN results AS tst2 ON tst2.RUNID = table.RUNID
  WHERE table.RUNID IN
  (
    ...// nothing important but in the end i get my tst2.Value differently
  )
 ) b