简体   繁体   English

MySQL在where子句中使用列别名

[英]Mysql use column alias in where clause

I have a Mysql Query: 我有一个Mysql查询:

SELECT created_at as date FROM table

So I created an alias for the created_at column. 因此,我为created_at列创建了一个别名。 Now I want to use this alias in my WHERE clause: 现在,我想在我的WHERE子句中使用此别名:

SELECT created_at as date FROM table WHERE date = 'xxxx-xx-xx'

This does not work. 这是行不通的。 Mysql expects me to use the real column name. Mysql希望我使用真实的列名。 Is there any way to do it the way I want to? 有什么办法可以按照我的意愿去做吗?

Background: I am selecting records from several tables and unite them using UNION . 背景:我正在从几个表中选择记录,并使用UNION它们UNION All the tables have a date column but it's named different in every table. 所有表都有一个日期列,但每个表中的名称都不同。 But I want to do WHERE on the united records and therefore I need to have always the same column name 但是我想在统一记录上执行WHERE操作,因此我需要始终使用相同的列名

If you absolutely have to use your alias as part of the selection criteria, then you need to use HAVING rather than WHERE 如果您绝对必须使用别名作为选择条件的一部分,那么您需要使用HAVING而不是WHERE

SELECT created_at as date FROM table HAVING date = 'xxxx-xx-xx'

But it's better to use the column name and WHERE because other people reading your code will find the meaning of WHERE more obvious than HAVING ; 但是最好使用列名和WHERE因为其他人在阅读您的代码时会发现WHERE的含义比HAVING更明显; and it won't use your table indexes as efficiently either 而且它也不会有效地使用您的表索引

You asked, "Is there any way to do it the way I want to?" 您问:“有什么方法可以按照我想要的方式进行吗?”

Yes, but it requires use of subquery, or a view. 是的,但是需要使用子查询或视图。

SELECT date 
FROM  (SELECT created_at as date FROM table) B
WHERE date = 'xxxx-xx-xx'

With the case of a union... 以工会为例...

SELECT * from (
Select col1, col2,col3 as date from A
union
select col1, col4,col5 from B) inlineView
WHERE date = 'somedate'

The problem you're running into is the order of operation within the database. 您遇到的问题是数据库内的操作顺序。

  1. FROM clause FROM子句
  2. WHERE clause WHERE子句
  3. GROUP BY clause GROUP BY子句
  4. HAVING clause HAVING子句
  5. SELECT clause SELECT子句
  6. ORDER BY clause ORDER BY子句

(order taken from http://www.bennadel.com/blog/70-sql-query-order-of-operations.htm ) a different order was found MySQL Order of Operations? (顺序取自http://www.bennadel.com/blog/70-sql-query-order-of-operations.htm )找到了不同的顺序MySQL操作顺序?

so now I have to do some research to figure out which one is correct; 所以现在我必须做一些研究来找出哪一个是正确的; or if it varies by RDBMS... 还是因RDBMS而异...

So the where clause is executed before the select is evaluated. 因此,在评估select之前执行where子句。 Thus it doesn't know you aliased it yet. 因此,它还不知道您是否对其进行了别名。 you can fool the compiler by making the data columns the way you want in the from clause by doing a subselect. 您可以通过执行子选择,使数据列在from子句中成为所需的方式来欺骗编译器。

哪里对别名不起作用..最好使用table_name.column_namem,您可以在group by中使用列的别名。

  SELECT created_at  FROM table WHERE table.created_at = 'xxxx-xx-xx'  <--more readable, isn't it?

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

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