Would it be possible to list Enum from a table field through a query?

Asked

Viewed 315 times

4

I wonder if there is any way to bring the values of the inserted in the ENUM of a table column through a query.

For example, with this table:

CREATE TABLE shirts (
    name VARCHAR(40),
    size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);

It would have to list the values x-small, small, medium, large and x-large through a SELECT?

Observing: I don’t want to take the values of the data entered in the table, but the list that is within the ENUM

3 answers

7


The very documentation quote:

To determine all possible values for an ENUM column, use SHOW COLUMNS FROM tbl_name LIKE 'enum_col' and parse the ENUM Definition in the Type column of the output.

Free translation: to determine all possible values of an ENUM column use SHOW COLUMNS FROM tbl_name LIKE 'enum_col' and treat the ENUM definition in the Type column of the result.

For your example:

SHOW COLUMNS FROM shirts LIKE 'size';

Getting the result:

| Field | Type                                               | Null | Key | Default | Extra |
| ----- | -------------------------------------------------- | ---- | --- | ------- | ----- |
| size  | enum('x-small','small','medium','large','x-large') | YES  |     |         |       |

See working on DB-Fiddle

4

You can use the table information information_schema.columns separating the information as suggested in the answer to the question Split column into multiple rows (split).

Schema (Mysql v5.7)

CREATE TABLE shirts (
  name VARCHAR(40),
  size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
);

CREATE TABLE numeros (
  numero int
);

INSERT INTO numeros(numero)
VALUES(1), (2), (3), (4), (5),
      (6), (7), (8), (9), (10);

Query

SELECT DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(REPLACE(REPLACE(REPLACE(c.column_type, 'enum(', ''), ')', ''), '''', ''), ',', n.numero), ',', -1) AS valor
  FROM information_schema.columns c
 CROSS JOIN numeros n
  WHERE c.table_name = 'shirts'
    AND c.column_name = 'size';

Resulting in

| valor   |
| ------- |
| x-small |
| small   |
| medium  |
| large   |
| x-large |

See working on DB Fiddle.


INFORMATION_SCHEMA COLUMNS

The COLUMNS table provides information about Columns in Tables.

In free translation:

The table COLUMNS provides information about table columns.

-2

Use this script with PHP (just adapt to the language in which you are programming):

$table_name = "nome_da_sua_tabela";
$column_name = "nome_da_sua_coluna";
echo "<select name=\"$column_name\">";
$result = mysql_query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = '$table_name' AND COLUMN_NAME = '$column_name'")
or die (mysql_error());

$row = mysql_fetch_array($result);
$enumList = explode(",", str_replace("'", "", substr($row['COLUMN_TYPE'], 5, (strlen($row['COLUMN_TYPE'])-6))));

foreach($enumList as $value)
    echo "<option value=\"$value\">$value</option>";
echo "</select>";

Browser other questions tagged

You are not signed in. Login or sign up in order to post.