select语句如何工作或从数据库中检索数据

时间:2014-08-08 18:17:06

标签: mysql rdbms

这是查询

select emp1.EmployeeId as empss, EmployeeName, DepartmentId, IsActive, CreatedDate, Address, salary 
FROM employee as emp1 
where 4=(select count(emp.EmployeeId) as con from employee as emp where emp.EmployeeId < emp1.EmployeeId);

这里是表名员工,我得到的数字5行没有限制,因为写这个查询我在网上得到了一些帮助,但我的问题是选择的法规是如何工作的?

1) does it select the column first then filter the where clause ?
2) does it filter the where clause then select column ??

请解释 非常感谢提前

2 个答案:

答案 0 :(得分:1)

在MySQL中,条件(过滤)在选择列之前执行。以下是SELECT语句中存在的各种子句的执行顺序

  1. FROM(包括加入)
  2. WHERE
  3. SELECT(选择列)
  4. GROUP BY
  5. ORDER BY
  6. LIMIT
  7. 可以通过以下SQL

    进行验证
    SELECT 1 as cnt FROM employee WHERE cnt = 1;
    

    此语句将抛出错误,因为cnt是在SELECT子句中定义的,该子句在WHERE之后但在SQL之后执行

    SELECT 1 as cnt FROM employee GROUP BY cnt;
    

    会奏效。

答案 1 :(得分:0)

从员工中选择*;

  

这将返回表格&#39; employee&#39;

中的所有行

从员工中选择ID;

  

这将返回所有行,但只返回表格&#39; employee&#39;中的id列。

从员工中选择id,name,其中id为(1,2);

  

这将仅返回(1,2)中employee.id的匹配行,并且列将是id和name

&#39;来自&#39;将引导数据库转到该表,条件将过滤掉所需的行 - 通过遍历并仅打印所请求的列来进一步缩小范围

相关问题