简体   繁体   English

SQL缺少表达式

[英]SQL missing expression

Pretty new to SQL. SQL的新手。 Have to answer this question. 必须回答这个问题。 I have 2 tables. 我有2张桌子。 "employees" and "departments" “雇员”和“部门”

The question is. 问题是。 "The employee name, head of the department (HOD), and salary for all employees whose salary is not in the range of $5,000–$ 10,000" “所有工资不在$ 5,000到$ 10,000范围内的雇员的雇员姓名,部门负责人和工资”

This is what i've tried. 这就是我尝试过的。

select employee_name, hod, salary
from employees
where salary < 5000 and > 10000;

Says missing expression under '>' 10000; 说在'>'10000下缺少表达式;

Any ideas? 有任何想法吗? Do I need to join departments? 我需要加入部门吗?

You have a syntax error, the last line needs to be (column name specified each time): 您有语法错误,最后一行需要是(每次指定列名):

where salary < 5000 and salary > 10000;

But then it won't make sense as there's no value meeting these criteria. 但是那样就没有意义了,因为没有满足这些标准的价值。 You need the conditions to use or : 您需要使用条件or

where salary < 5000 or salary > 10000

to exclude salaries between 5000 and 10000. 排除5000至10000之间的薪水。

You can also use BETWEEN to achieve the same: 您还可以使用BETWEEN实现相同的目的:

where salary not between 5000 and 10000

Each criteria in the WHERE clause needs to stand on its own, for your 2nd criteria it doesn't know what field should be > 10000 , you have to specify: WHERE子句中的每个条件都必须独立存在,对于第二个条件,它不知道哪个字段应> 10000 ,因此必须指定:

select employee_name, hod, salary
from employees
where salary < 5000 
  and salary > 10000;

This will exclude all records since it's a per-row comparison and each record can only have one value of salary , so likely you want OR instead of AND , or maybe you have different fields in your actual query. 这将排除所有的记录,因为它是在每一行的比较和每个记录只能有一个价值salary ,所以有可能你想OR代替AND ,也许你有你的实际查询不同的领域。

There is BETWEEN for defining a range of numbers/dates, but it is inclusive ie WHERE salary BETWEEN 5000 and 10000 would include everything >= 5000 and <= 10000, so not helpful in this situation, and some prefer to avoid it altogether as it can be tricky with dates. BETWEEN定义一个数字/日期范围,但是它是包容性的,即WHERE salary BETWEEN 5000 and 10000将包括> = 5000和<= 10000的所有内容,因此在这种情况下没有帮助,有些人希望完全避免使用它约会可能会有些棘手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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