I've ran across a problem where I'm searching the database by a LIKE
query:开发者_运维问答
SELECT description,
title,
hidden
FROM paragraphs
WHERE description LIKE '%some search%'
OR title LIKE '%some search%'
AND hidden = 0
Even when it searches, it should completely ignore records if hidden
has a value of 1
in it, because I get returned hidden results.
How do you make a query that checks if hidden
is 0
while if it's 1
the query should skip it those records.
Put the like's in brackets.
SELECT description,
title,
hidden
FROM paragraphs
WHERE (description LIKE '%some search%' OR title LIKE '%some search%')
AND hidden = 0
You will need to re write ur query with parentheses
SELECT description,
title,
hidden
FROM paragraphs
WHERE
(description LIKE '%some search%' OR title LIKE '%some search%')
AND
hidden = 0
Now you it should work
SELECT description,
title,
hidden
FROM paragraphs
WHERE (
description LIKE '%some search%'
OR title LIKE '%some search%'
) AND hidden = 0
Your query's criteria section is ambiguous, and that's likely causing your issues. Group your criteria with parenthesis as appropriate. Here's what I think you want:
SELECT description,
title,
hidden
FROM paragraphs
WHERE
( description LIKE '%some search%' OR title LIKE '%some search%')
AND ( hidden = 0 )
(Show me all items that match either this string or that string, AND aren't hidden)
精彩评论