开发者

SQL OR Operator

I'm currently fiddling with O'Reilly's Learning SQL. Given the th开发者_C百科e following query, it returns 21 rows:

select account_id, product_cd, cust_id, avail_balance 
from account 
where 
product_cd = 'CHK' OR 
product_cd = 'SAV' OR 
product_cd = 'CD'  OR 
product_cd = 'MM'

Being used to BPFs I tried this, which returns 24 rows and 96 Warnings.

select account_id, product_cd, cust_id, avail_balance
from account
where product_cd = ('CHK' OR 'SAV' OR 'CD' OR 'MM');

I know, one could use the IN operator but, why does the second query return 3 more rows?


I am guessing that there is some implicit conversion involved and ('CHK' OR 'SAV' OR 'CD' OR 'MM') is being evaluated as a boolean. This does not seem valid on SQL Server, e.g., I cannot do:

select ('CHK' OR 'SAV' OR 'CD' OR 'MM')


If this is MySql, I think you will find that your second query is returning a result equivilent to 'where product_cd = 1' which just so happens to be 3 more rows then the first query, the second query isn't correct


If you are learning SQL, i.e. ANSI SQL, you should set MySQL to use ANSI confomance, or use a really ANSI conformant product that catches your errors and doesn't return psuedo-results. The expression below:

('CHK' OR 'SAV' OR 'CD' OR 'MM')

is plain error in SQL, as OR can only be used with boolean values, for example the result of a comparison.

MySQL is "converting" the strings into integers and suchlike, then comparing the result of the expression with the product_cd. The result of the query is useless.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜