sql: and ... or ...
following sql is just meaning - wrong in syntax:
select * from ... where user_id <> 10 and (name='hello' or name='good');
brace above makes "or" has higher priority than "and" - similar to 5x(2+3) in math.
do I have to write user_id <> 10 for twice in real sql as bellow?
select * from ... where user_id <> 10 and name='hello' or user_id <> 10 and name='good';
Any comments to make the sql simpler?
select * from ... where user_id <> 10 and (name='hello' or name='good'); is as simple as it gets. Another way of doing it would be:
select * from ... where (user_id <> 10 and name='hello') or (user_id <> 10 and name='good');
Another option which might become useful if the list of possible values for "name" increases:
SELECT * FROM table_name WHERE user_id <> 10 AND name NOT IN('hello', 'good', 'foo', 'bar')
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)