2016-05-16 87 views
2

我想寫書,查詢和=條件:Postgres的查詢與LIKE和不等於

SELECT * 
FROM posts 
WHERE title LIKE 'term%' 
    OR NAME LIKE 'term%' 
AND post_type != 'type'; 

然而,查詢結果將不被post_type過濾。我的語法有什麼問題嗎?

回答

2

您可能需要括號,因爲AND具有運算符優先級。

SELECT * 
FROM posts 
WHERE (title LIKE 'term%' OR NAME LIKE 'term%') 
    AND post_type != 'type'; 

因爲現在沒有括號你有

SELECT * 
FROM posts 
WHERE title LIKE 'term%' 
    OR (  NAME LIKE 'term%' 
     AND post_type != 'type'); 
+0

這就是絕招!謝謝。如果可以的話,我會投票贊成 – nateM

+0

如果有足夠的代表,您可以接受答案並註冊成功。 –