SQL选择联接与一列相同的2个表

时间:2019-02-03 05:08:18

标签: sql-server tsql

SQL表const char* gradeCheck(double mark) { char buffer [100]; if (mark > MAX_MARK || mark < MIN_MARK) { sprintf(buffer, "INVALID TYPE\n"); return buffer; } else if (mark >= PASSING_MARK && mark <= MAX_MARK) { sprintf(buffer, "Student achieved %.2lf\n", mark); return buffer; } else { sprintf(buffer, "Student achieved %.2lf which means they failed ;(\n", mark); return buffer; } } 是:

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中的至少一个为1

AuthorNo.Prepay

如何选择?

2 个答案:

答案 0 :(得分:0)

我猜您只是想在结果中看到AuthorNo?试试这个

Select distinct a.AuthorNo
From Authority  a
join Order  b on a.AuthorNo=b.AuthorNo
where a.Prepay=1

答案 1 :(得分:0)

如果您需要查找具有由PrePay作者订购的订单的作者?

然后您可以为此使用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;

测试contains

结果:

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