更高效的代码使用运算符

时间:2013-11-21 03:39:21

标签: mysql sql

你好,其他程序员再次

Customer

cust_nbr
cust_name
cust_street_address
Cust_City
Cust_Zip
Cust_industry_code

我想选择:客户编号,客户名称,客户邮编和客户行业代码,适用于行业代码在1000到2000之间或超过5000的所有客户,然后按行业代码对输出进行排序。

这是我的尝试:

select CUST_NBR, CUST_NAME,CUST_ZIP, CUST_INDUSTRY_CODE 
  from customer 
 where CUST_INDUSTRY_CODE between 1000 and 2000 
    or CUST_INDUSTRY_CODE>5000 
 order by CUST_INDUSTRY_CODE ;
你知道它有什么问题吗?有人提到有一个小问题,但我无法发现它,因为它似乎给了我我需要的东西。

感谢你。

1 个答案:

答案 0 :(得分:0)

SELECT cust_nbr,
cust_name,
cust_zip,
cust_industry_code
FROM customer
WHERE 
    (cust_industry_code >= 1000 AND cust_industry_code <= 2000) 
    OR cust_industry_code > 5000
ORDER BY cust_industry_code;

如果您愿意,可以使用BETWEEN语句。如果在此处使用大于等于的版本,则需要括号。我建议他们不要将您的意图传达给稍后查看此代码的人。

更常见的是大写SQL命令并使用小写字母,但这只是一个风格点。无论哪种方式都可以。