2017-10-04 53 views
1

我想一個NOT子句添加到該SQL查詢:如何添加一個NOT子句到我的SQL查詢

Select 
    members.Member_Id, 
    members.Title, 
    members.FirstName, 
    members.LastName, 
    members.Po_Box, 
    members.Street, 
    members.City, 
    members.Del, 
    members.Mobile, 
    members.eMail, 
    members.WFTD, 
    ship_info.Renewal_Date 
From 
    members 
Left Join 
    ship_info on (members.Member_Id = ship_info.Member_Id) 
Order By 
    ship_info.Renewal_Date 

未條款是這樣的:

Where Member_Id Not Between 2000 And 3000; 

我曾嘗試將這一行放在不同的地方,但每次都會出現錯誤,並且由於Wamp用法語報告錯誤,因此找不到任何幫助。

是的,類似這樣的問題,但他們更困惑我,因爲我不明白他們足以相應地修改我的腳本。我不太瞭解數組和複雜的代碼。

爲了郵件合併的目的,我從數據庫中導出數據,並且其成員身份在2000年代的成員被刪除成員,其ID已從其原始ID移動到更高數字的成員,因爲我不想永久刪除人員以防他們後來改變主意。

謝謝。

+2

你'Member_Id'列在查詢的兩個表存在。你需要指定表名。 –

+0

您需要在Member_id前面添加一個表名,因爲它是有意義的。 – orhtej2

+0

如果您指定了任何錯誤,那麼會更好,否則在where子句中會出現一個錯誤,您必須指定表別名,因爲兩個表中都存在「Member_id」 –

回答

1

由於您正在使用JOIN並且member_id字段駐留在JOIN的兩個表中,所以必須指定您需要where語句的表。嘗試:

SELECT members.Member_Id 
    ,members.Title 
    ,members.FirstName 
    ,members.LastName 
    ,members.Po_Box 
    ,members.Street 
    ,members.City 
    ,members.Del 
    ,members.Mobile 
    ,members.eMail 
    ,members.WFTD 
    ,ship_info.Renewal_Date 
FROM members 
LEFT JOIN ship_info ON (members.Member_Id = ship_info.Member_Id) 
WHERE members.Member_Id NOT BETWEEN 2000 
     AND 3000 
ORDER BY ship_info.Renewal_Date 
2

你可以嘗試這樣的:

Select members.Member_Id 
    ,members.Title 
    ,members.FirstName 
    ,members.LastName 
    ,members.Po_Box 
    ,members.Street 
    ,members.City 
    ,members.Del 
    ,members.Mobile 
    ,members.eMail 
    ,members.WFTD 
    ,ship_info.Renewal_Date 
From members 
LEFT JOIN ship_info ON (members.Member_Id=ship_info.Member_Id) 

WHERE (members.Member_ID < 2000 OR members.member_ID > 3000) 

ORDER BY ship_info.Renewal_Date 
0

我覺得下面應該工作,如果沒有的話,請註明錯誤,你得到了,你只需要前綴表名members(如果更好您使用此表名作爲別名)

SELECT members.Member_Id 
    ,members.Title 
    ,members.FirstName 
    ,members.LastName 
    ,members.Po_Box 
    ,members.Street 
    ,members.City 
    ,members.Del 
    ,members.Mobile 
    ,members.eMail 
    ,members.WFTD 
    ,ship_info.Renewal_Date 
FROM members 
LEFT JOIN ship_info ON (members.Member_Id = ship_info.Member_Id) 
WHERE members.Member_Id NOT BETWEEN 2000 
     AND 3000 
ORDER BY ship_info.Renewal_Date 
1

據「數據庫管理基礎」,下面將是適當的語法:

Select members.Member_Id 
    ,members.Title 
    ,members.FirstName 
    ,members.LastName 
    ,members.Po_Box 
    ,members.Street 
    ,members.City 
    ,members.Del 
    ,members.Mobile 
    ,members.eMail 
    ,members.WFTD 
    ,ship_info.Renewal_Date 
From members 
LEFT JOIN ship_info ON (members.Member_Id=ship_info.Member_Id) 

WHERE NOT members.Member_Id BETWEEN 2000 AND 3000 

ORDER BY ship_info.Renewal_Date ; 

在前面提到的書,它指出,

JOIN報表既可以在FROMWHERE 子句指定,但建議您指定他們在條款 「。

至於第優先級,這裏是正確的順序如下:

SELECT  -- "what" we want 
FROM  -- "where" to look for it 
JOIN 
WHERE  -- condition 
GROUP BY 
HAVING 
ORDER BY -- sorting order 
相關問題