How to create virtual column using MySQL SELECT?

前端 未结 6 1914
天命终不由人
天命终不由人 2020-12-07 13:31

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 quer

6条回答
  •  独厮守ぢ
    2020-12-07 14:03

    Something like:

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

    This allows you to make operations and show it as columns.

    EDIT:

    you can also use joins and show operations 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
    

提交回复
热议问题