Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to return enum values in MySQL?

Tags:

enums

mysql

I need to select the enum values of a column. From searching I've found two ways:

SELECT column_type FROM information_schema.columns 
WHERE table_name = 'MyTable' AND column_name = 'MyColumn';

and the other:

SHOW COLUMNS FROM `mytable` WHERE field = 'type';

Although the first query will give me this info:

enum('value1','value2','value3')

The second query gives me the same and with additional columns as well. I would prefer to just get those values without the enum() and commas, is it possible, or do I need to parse the values out? Not that it's hard just checking if there is an easier way.

Assuming there is no easier way, which of the two queries above is better to use? I noticed that the second query doesn't show the query time when I ran it, I almost thought it didn't require any time at all. But if I turn on the profiler I can see that it does take time, but it seem a bit faster. So would the second query be more efficient?

like image 970
Joker Avatar asked Jan 10 '11 06:01

Joker


People also ask

How can I get enum possible values in a MySQL database?

$syntax = mysql_query("SELECT COLUMN_TYPY FROM information_schema. `COLUMNS` WHERE TABLE_NAME = '$table' AND COLUMN_NAME ='$colm'"); if (! mysql_error()){ //Get a array possible values from table and colm.

What is correct usage of enum in MySQL?

The ENUM data type in MySQL is a string object. It allows us to limit the value chosen from a list of permitted values in the column specification at the time of table creation. It is short for enumeration, which means that each column may have one of the specified possible values.

How do I return an enum list?

The idea is to use the Enum. GetValues() method to get an array of the enum constants' values. To get an IEnumerable<T> of all the values in the enum, call Cast<T>() on the array. To get a list, call ToList() after casting.

Does MySQL support enum?

In MySQL, an ENUM is a string object whose value is chosen from a list of permitted values defined at the time of column creation. The ENUM data type provides the following advantages: Compact data storage. MySQL ENUM uses numeric indexes (1, 2, 3, …) to represents string values.


1 Answers

I guess you can't select those values out, I ended up parsing the values out with this:

$result = str_replace(array("enum('", "')", "''"), array('', '', "'"), $result);
$arr = explode("','", $result);
return $arr;
like image 99
Joker Avatar answered Oct 20 '22 00:10

Joker