tags:

views:

2691

answers:

4

Hi,

If I do SELECT a AS b and b is not a column in the table, would query create the "virtual" column?

in fact, I need to incorporate some virtual column into the query and process some information into the query so I can use it with each item later on.

+4  A: 

something like

    SELECT id, email, IF(actived = 1, 'enabled', 'disabled') AS account_status
FROM users

this allow you to make operations and show it as columns.

EDIT:

you can also use joins and show operaitons as columns:

    SELECT u.id, e.email, IF(c.id IS NULL, 'no selected', c.name) as country
FROM users u LEFT JOIN countries c ON u.country_id = c.id
Gabriel Sosa
This post moves my knowledge of simple mysql statements a way forward :) , does it mean I can create ANY "column"??
Skuta
A: 

SELECT only retrieves data from the database, it does not change the table itself.

If you write

SELECT a AS b FROM x

"b" is just an alias name in the query. It does not create an extra column. Your result in the example would only contain one column named "b". But the column in the table would stay "a". "b" is just another name.

I don't really understand what you mean with "so I can use it with each item later on". Do you mean later in the select statement or later in your application. Perhaps you could provide some example code.

Sebastian Dietz
A: 

Your syntax would create an alias for a as b, but it wouldn't have scope beyond the results of the statement. It sounds like you may want to create a VIEW

cmsjr
A: 

interesting. Also beside this subject, I'd like to sort a table by "money" values desc. But I'd like to add a virtual column to this sort, with counting just to have a reference about my looking value, in sorted column and to check what's next below value or above my value.

How can I add that virtual counting values to my new sort, because then I want to manage values from my virtual column.

antonio1