Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: use a value from a select in the select itself

First, if someone has a better title please help.

If I have let's say a 'calendar' table with a 'day' column. And I have the following query:

SELECT day, day AS testDay, testDay AS test2Day FROM calendar

MySQL will complain that "testDay" is an unknown column. Of course you will tell me that this statement is useless, but my statement looks more like this:

SELECT day, SOME_CRAZY_EXPRESSION_OF(day) AS testDay, EXPRESSION_OF(testDay) AS test2Day FROM calendar

And the point is I don't want to evaluate twice the first expression to use it within the second expression.. So is there a way to use a value calculated in the select as part of the select itself?

Of course I could do:

SELECT day, SOME_CRAZY_EXPRESSION_OF(day) AS testDay, EXPRESSION_OF(SOME_CRAZY_EXPRESSION_OF(day)) AS test2Day FROM calendar

But I'm trying to avoid wasting. If I have no choice, that's what I'll do.

like image 218
Nathan H Avatar asked Jan 29 '12 11:01

Nathan H


People also ask

Can we use SELECT inside SELECT in MySQL?

In MySQL subquery can be nested inside a SELECT, INSERT, UPDATE, DELETE, SET, or DO statement or inside another subquery. A subquery is usually added within the WHERE Clause of another SQL SELECT statement.

How do you assign a selected value to a variable in MySQL?

The syntax for assigning a value to a SQL variable within a SELECT query is @ var_name := value , where var_name is the variable name and value is a value that you're retrieving. The variable may be used in subsequent queries wherever an expression is allowed, such as in a WHERE clause or in an INSERT statement.

What is the difference between SELECT * and SELECT?

Save this answer. Show activity on this post. select * will return values for all the columns in the table that have rows that match your predicate, while select column_name will only return the values in the column_name column for the rows that match your predicate. Save this answer.

Why is SELECT * used in MySQL?

The SELECT statement is used to select data from a database. The data returned is stored in a result table, called the result-set.


2 Answers

So, you can't use variables, because they are containing value from the previous row (from official docs: https://dev.mysql.com/doc/refman/5.0/en/user-variables.html):

SELECT (@aa:=id) AS a, (@aa+3) AS b FROM tbl_name HAVING b=5;

The reference to b in the HAVING clause refers to an alias for an expression in the select list that uses @aa. This does not work as expected: @aa contains the value of id from the previous selected row, not from the current row.

And thats why i founded a temporary solution that is based on the code fragments. I'll show an example in PHP:

$querySubstitutions = array(
      '%days%' => 'TO_DAYS(table.started_at + INTERVAL (table.period - 1) DAY)'
    );

$query = 'SELECT %days% AS days, <EXPRESSION_OF( %days% )> FROM table';

// Replacing pairs in a query
$query = strtr($query, $querySubstitutions);

This solution helps to compact a query in most cases, but i don't know yet how it affects performance.

like image 137
Dmitriy Avatar answered Oct 12 '22 02:10

Dmitriy


In MySQL, you'll want to assign a temporary variable to the value of the expression to be able to reuse it, this should do it;

SELECT day, @tmp:=SOME_CRAZY_EXPRESSION_OF(day) AS testDay, 
    EXPRESSION_OF(@tmp) AS test2Day FROM calendar

See this page for another example with more analysis on the performance impact.

like image 24
Joachim Isaksson Avatar answered Oct 12 '22 00:10

Joachim Isaksson