SQL SELECT加入2代表相同的一列

问题描述 投票:-2回答:2

SQL表Authority是:

AuthorNo  Price  PrePay(bit)
----------------------------
   1       250$    1
   2       120$    0
   3       300$    0
   4       112$    1
   5       25$     0

Order是:

AuthorNo  OrderNo
-----------------
  1         33
  1         34
  2         33
  2         38
  3         41
  3         82
  4         55
  4         21
  5         21
  5         66

我想要的结果是:

Authority.AuthorNo选取AuthorNoOrder.OrderNo相同和AuthorNo.Prepay中的至少一个是1

AuthorNo
--------
   1
   2
   4
   5

如何选择呢?

sql-server tsql
2个回答
0
投票

如果你需要找到具有该订单还通过与预付费订购作者的作者?

然后,你可以使用一个EXISTS这一点。

SELECT auth.AuthorNo
FROM Authority auth
JOIN [Order] ord ON ord.AuthorNo = auth.AuthorNo
WHERE EXISTS
(
    SELECT 1
    FROM [Order] ord_pp 
    JOIN Authority auth_pp 
      ON auth_pp.AuthorNo = ord_pp.AuthorNo 
     AND auth_pp.Prepay = 1
    WHERE ord_pp.OrderNo = ord.OrderNo
)
GROUP BY auth.AuthorNo;

测试here

结果:

AuthorNo
--------
1
2
4
5

0
投票

我猜你只是想看看AuthorNo的结果呢?尝试这个

Select distinct a.AuthorNo
From Authority  a
join Order  b on a.AuthorNo=b.AuthorNo
where a.Prepay=1
© www.soinside.com 2019 - 2024. All rights reserved.