
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
MySQL query to select ENUM('M', 'F') as 'Male' or 'Female'?
You can use IF() for this. Let us first create a table. One of the columns here is having ENUM type
mysql> create table DemoTable ( UserId int, UserName varchar(40), UserGender ENUM('M','F') ); Query OK, 0 rows affected (1.11 sec)
Insert records in the table using insert command −
mysql> insert into DemoTable values(1,'John','M'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(2,'Maria','F'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(3,'David','M'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(4,'Emma','F'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+--------+----------+------------+ | UserId | UserName | UserGender | +--------+----------+------------+ | 1 | John | M | | 2 | Maria | F | | 3 | David | M | | 4 | Emma | F | +--------+----------+------------+ 4 rows in set (0.00 sec)
Following is the query to select ENUM('M', 'F') as 'Male' or 'Female'−
mysql> SELECT UserId,UserName,IF(UserGender='F','Female', 'Male') AS `UserGender` from DemoTable;
This will produce the following output−
+--------+----------+------------+ | UserId | UserName | UserGender | +--------+----------+------------+ | 1 | John | Male | | 2 | Maria | Female | | 3 | David | Male | | 4 | Emma | Female | +--------+----------+------------+ 4 rows in set (0.00 sec)
- Related Articles
- Which mosquitoes bite? Female or Male?
- C++ code to find out if a name is male or female
- MySQL query to sort by both timestamp and enum?
- Priority of AND and OR operator in MySQL select query?
- Select the table name as a column in a UNION select query with MySQL?
- Describe female and male gametes in flower.
- MySQL query to select too many rows?
- MySQL query to select date from timestamp?
- MySQL query to select top 10 records?
- MySQL query to select bottom n records
- MySQL query to select multiple rows effectively?
- MySQL syntax error (in SELECT query) while using ‘group’ as table name
- Mention male and female part of a plant.
- Name the male and female gametes in animals.
- MySQL query to select column where value = one or value = two, value = three, etc?

Advertisements