简体   繁体   中英

Sql or operator

I have something I don't understand. If my table is going like this:

name   age 
----   ----
din     18
mari    35

And i did this query:

select * from usernames
where age < 20 or name not like 'din'

And all the table and rows printed, my goal is to print who was not called 'din' and under the age of 20.

Can someone tell me where is my mistake?

i asked here another q .. sorry about it.

Explanation:

where age < 20 or name not like 'din'

breaks down to:

where <something> or <something else>

For each row, < something > and < something else > will be evaluated. The or means the result will be true if either is true. Only one of them needs to be true for the row to be selected.

In your example, the first row meets the age < 20 condition. The rest of the test is not important (and it evaluates as false) because we already have a true. The second row evaluates as false for age < 20 but true for name not like 'din' so as explained above, if either side of the OR is true, the result is true, and thus row 2 is returned in the results.

That is why both rows are returned.

Add this data to your table to help illustrate:

name   age 
----   ----
din     28  (both sides false)
mari    15  (both sides true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM