2017-03-16 111 views
0

我目前是一名學生,我正試圖解決SQL的一個小問題。我想根據來自同一個表的過濾結果在表上執行更新語句。 Here is the table I have:SQL更新語句與where子句中的子查詢相同的表

這是查詢我到現在:

UPDATE ComplexityLessons 
SET ACADEMYRATIO = '0,896' 
WHERE (
    SELECT c.START, c.DATE, c.ACTIVITY, c.ACADEMY 
from ComplexityLessons c 
join (
    SELECT b.ACTIVITY, b.START, b.DATE 
    FROM ComplexityLessons b 
) as b 
on c.ACTIVITY = b.ACTIVITY and c.DATE = b.DATE and c.START = b.START 
having COUNT(DISTINCT ACADEMY) = 2) 

所以,總結我想更新列「ActivityRatio」僅在2所院校在同一時間進行同樣的活動。當我僅使用子查詢時,我得到了同時執行相同活動的院校,但我無法弄清楚更新聲明。 預先感謝您的幫助!

+1

您使用MySQL,MS SQL Server或Oracle嗎?不要標記不涉及的產品! – jarlh

+0

@jarlh:我使用phpmyadmin,所以MySQL。 –

回答

0
UPDATE ComplexityLessons SET ACADEMYRATIO = '0,896' 
WHERE (SELECT c.START, c.DATE, c.ACTIVITY, c.ACADEMY 
     from ComplexityLessons c 
join (SELECT b.ACTIVITY, b.START, b.DATE 
     FROM ComplexityLessons b) as b 
on c.ACTIVITY = b.ACTIVITY and c.DATE = b.DATE and c.START = b.START 
having COUNT(DISTINCT ACADEMY) = 2) 

在哪裏應該有條件和子查詢應該返回ID。 例如

UPDATE ComplexityLessons SET ACADEMYRATIO = '0,896' 
WHERE <id> in (SELECT <id> from ComplexityLessons c 
join (SELECT b.ACTIVITY, b.START, b.DATE 
     FROM ComplexityLessons b) as b 
on c.ACTIVITY = b.ACTIVITY and c.DATE = b.DATE and c.START = b.START 
having COUNT(DISTINCT ACADEMY) = 2) 

的應該是表之間的foriegn鍵。

+0

我試過了,但它仍然給我錯誤「操作數應該只包含一列」 –

+0

請提供您使用的最新查詢。 –

+0

UPDATE ComplexityLessons SET ACADEMYRATIO ='0,896'WHERE ACTIVITY IN(SELECT c.START,c.DATE,c.ACTIVITY,c.ACADEMY from ComplexityLessons c join(SELECT b.ACTIVITY,b.START,b.DATE FROM ComplexityLessons b)作爲b對c.ACTIVITY = b.ACTIVITY和c.DATE = b.DATE和c.START = b.START有COUNT(DISTINCT ACADEMY)= 2)@Sambi Reddy –