How to create virtual column using MySQL SELECT?


Question

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.

1
59
6/22/2011 7:59:24 PM

Accepted Answer

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
79
1/18/2009 3:07:48 PM

Try this one if you want to create a virtual column "age" within a select statement:

select brand, name, "10" as age from cars...

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon