SQL查询在结果上添加行 - sql server 2012

时间:2016-10-28 07:32:14

标签: sql sql-server tsql sql-server-2012

在我的Order表中,我为每个Order有三个任务(id_task)。如果id_address与id_address 14匹配,那么我想显示该特定id_order的所有任务。

id_order | id_task | id_address
 1       |   556   | 12
 1       |   557   | 14 
 1       |   558   | 11
 2       |   559   | 56
 2       |   560   | 88 
 2       |   561   | 77
当我匹配id_address = 14

时,

结果

id_order | id_task | id_address
 1       |   556   | 12
 1       |   557   | 14 
 1       |   558   | 11

我该怎么做?

我现在这样做:

select id_order, id_task, id_address
from order
where id_address = 14

当然这不起作用。我只会得到任务557.但是我也希望得到id_order的其他结果。但是如何?

3 个答案:

答案 0 :(得分:3)

这是一个简单的解决方案:

select 
      id_order
    , id_task
    , id_address 
from 
    [order] 
where 
    id_order IN (
            select 
                id_order 
            from 
                [order] 
            where   
                id_address = 14)

答案 1 :(得分:2)

您可以使用 compile "org.grails.plugins:hibernate4" compile "org.hibernate:hibernate-core:4.3.11.Final" compile "org.hibernate:hibernate-ehcache:4.3.10.Final" compile 'org.grails:grails-datastore-core:5.0.12.RELEASE' compile 'org.grails:grails-datastore-simple:5.0.12.RELEASE' compile 'org.grails:grails-datastore-gorm-support:5.0.12.RELEASE' compile 'org.grails:grails-datastore-gorm:5.0.12.RELEASE' compile 'org.grails:grails-datastore-gorm-hibernate4:5.0.12.RELEASE' compile 'org.grails:grails-datastore-gorm-hibernate-core:5.0.12.RELEASE'

EXISTS

答案 2 :(得分:0)

您可以如下所示:

SELECT 
    A.* 
FROM 
    Order A INNER JOIN 
    Order B ON A.id_order = B.id_order 
WHERE
    B.id_address = 14