如何将INNER JOIN中编写的代码转换为Subquery

时间:2014-10-21 04:40:18

标签: mysql sql sql-server

将代码从Join语句转换为Subquery需要一些帮助。 我需要以某种方式从它中删除GROUP BY,当转换为子查询并且不知道如何。 管理将小部分子查询放在代码的末尾,不知道如何休息。 需要一些帮助,谢谢。

以下是代码示例:(需要转换为SQL Server语法)

SELECT 
     b.Number, t.IDTyre, SUM(c.Price)
FROM Tyre AS t
INNER JOIN Bill AS b ON t.BillID = b.IDBill
INNER JOIN Customer AS c ON c.TyreID = t.IDTyre
GROUP BY b.Number, t.IDTyre
HAVING SUM(c.Price) < 3000 OR t.IDTyre NOT IN (SELECT c.TyreID FROM Customer AS c)

2 个答案:

答案 0 :(得分:0)

检查以下查询是否有效:

SELECT 
     (Select b.Number From Bill AS b Where b.IDBill = t.BillID) as Number,
     t.IDTyre as TyreID,
     (Select SUM(c.Price) From Customer AS c Having SUM(c.Price) < 3000 OR t.IDTyre NOT IN (SELECT Distinct c.TyreID FROM Customer AS c) And c.TyreID = t.IDTyre) as Price 
FROM Tyre AS t

答案 1 :(得分:0)

您为什么要尝试将其转换为子查询?

JOINS是处理链接表时的最佳选择。

也是&#34; NOT IN&#34;你最后想要做的也不好,你应该使用&#34; NOT EXISTS&#34;。将其更改为:OR NOT EXISTS(SELECT * FROM Customer AS c WHERE t.IDTyre = c.TyreID)