Programing

MySQL 데이터베이스에서 가능한 열거 형 값을 얻으려면 어떻게해야합니까?

lottogame 2020. 9. 4. 08:01
반응형

MySQL 데이터베이스에서 가능한 열거 형 값을 얻으려면 어떻게해야합니까?


DB에서 가능한 열거 형 값으로 드롭 다운을 자동으로 채우고 싶습니다. 이것이 MySQL에서 가능합니까?


당신을위한 codeigniter 버전이 있습니다. 또한 값에서 따옴표를 제거합니다.

function get_enum_values( $table, $field )
{
    $type = $this->db->query( "SHOW COLUMNS FROM {$table} WHERE Field = '{$field}'" )->row( 0 )->Type;
    preg_match("/^enum\(\'(.*)\'\)$/", $type, $matches);
    $enum = explode("','", $matches[1]);
    return $enum;
}

다음과 같이 쿼리하여 값을 가져올 수 있습니다.

SELECT SUBSTRING(COLUMN_TYPE,5)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA='databasename' 
    AND TABLE_NAME='tablename'
    AND COLUMN_NAME='columnname'

거기에서 배열로 변환해야합니다.

  • 게으른 경우 배열로 직접 평가하십시오 (MySQL의 작은 따옴표 이스케이프는 호환되지 않을 수 있음).
  • $ options_array = str_getcsv ($ options, ',', " '") 아마도 작동 할 것입니다 (여는 괄호와 닫는 괄호를 건너 뛰도록 하위 문자열을 변경 한 경우) 또는
  • 정규식

MySQL 참조

ENUM 열에 대해 가능한 모든 값을 확인하려면 SHOW COLUMNS FROM tbl_name LIKE enum_col을 사용하고 출력의 Type 열에서 ENUM 정의를 구문 분석합니다.

다음과 같은 것을 원할 것입니다.

$sql = "SHOW COLUMNS FROM `table` LIKE 'column'";
$result = $db->query($sql);
$row = $result->fetchRow();
$type = $row['Type'];
preg_match('/enum\((.*)\)$/', $type, $matches);
$vals = explode(',', $matches[1]);

이렇게하면 인용 된 값이 제공됩니다. MySQL은 항상 작은 따옴표로 묶인이를 반환합니다. 값의 작은 따옴표는 작은 따옴표로 이스케이프됩니다. trim($val, "'")각 배열 요소를 안전하게 호출 할 수 있습니다 . 당신은 변환 할 수 있습니다 ''단지에 '.

다음은 따옴표없이 $ trimmedvals 배열 항목을 반환합니다.

$trimmedvals = array();
foreach($vals as $key => $value) {
$value=trim($value, "'");
$trimmedvals[] = $value;
}

이것은 위의 많은 것과 비슷하지만 루프가없는 결과를 제공하고 원하는 결과를 얻을 수 있습니다. 선택 옵션을 생성하기위한 간단한 배열입니다.

보너스 : SET 및 ENUM 필드 유형에 대해 작동합니다.

$result = $db->query("SHOW COLUMNS FROM table LIKE 'column'");
if ($result) {
    $option_array = explode("','",preg_replace("/(enum|set)\('(.+?)'\)/","\\2", $result[0]->Type));
}

$ option_array : 배열 ([0] => 빨간색 [1] => 녹색 [2] => 파란색)


이것은 Chris Komlenic의 MySQL의 ENUM 데이터 유형이 나쁜 8 가지 이유 중 하나입니다 .

 4. 고유 한 ENUM 구성원 목록을 얻는 것은 고통 스럽습니다.

매우 일반적인 요구 사항은 데이터베이스에서 가능한 값으로 선택 상자 또는 드롭 다운 목록을 채우는 것입니다. 이렇게 :

색상 선택 :

[ select box ]

이러한 값이 'colors'라는 참조 테이블에 저장되는 경우 필요한 것은 다음과 같습니다. SELECT * FROM colors... 그런 다음 파싱하여 드롭 다운 목록을 동적으로 생성 할 수 있습니다. 참조 테이블에서 색상을 추가하거나 변경할 수 있으며 섹시한 주문 양식이 자동으로 업데이트됩니다. 대박.

이제 사악한 ENUM을 고려하십시오. 멤버 목록을 어떻게 추출합니까? DISTINCT 값에 대해 테이블의 ENUM 열을 쿼리 할 수 ​​있지만 모든 가능한 값 이 아니라 실제로 사용되어 테이블에 존재하는 값만 반환 됩니다. INFORMATION_SCHEMA를 쿼리하고 스크립팅 언어를 사용하여 쿼리 결과에서 구문 분석 할 수 있지만 불필요하게 복잡합니다. 사실, 나는 ENUM 열의 멤버 목록을 추출하는 우아하고 순수한 SQL 방법을 모릅니다.


CSV (Comma Separated Value) 문자열 인 것처럼 문자열을 구문 분석 할 수 있습니다. PHP에는 CSV 문자열을 배열로 변환하는 str_getcsv라는 훌륭한 내장 함수가 있습니다.

// This is an example to test with
$enum_or_set = "'blond','brunette','redhead'";

// Here is the parser
$options = str_getcsv($enum_or_set, ',', "'");

// Output the value
print_r($options);

이렇게하면 다음과 유사한 내용이 표시됩니다.

Array
(
    [0] => blond
    [1] => brunette
    [2] => redhead
)

이 메서드를 사용하면 문자열에 작은 따옴표를 사용할 수도 있습니다 (두 개의 작은 따옴표 사용에 유의하십시오).

$enum_or_set = "'blond','brunette','red''head'";

Array
(
    [0] => blond
    [1] => brunette
    [2] => red'head
)

str_getcsv 함수에 대한 자세한 내용은 PHP 설명서를 확인하십시오. http://uk.php.net/manual/en/function.str-getcsv.php


더 최신의 방법으로 나를 위해 일했습니다.

function enum_to_array($table, $field) {    
    $query = "SHOW FIELDS FROM `{$table}` LIKE '{$field}'";
    $result = $db->query($sql);
    $row = $result->fetchRow();
    preg_match('#^enum\((.*?)\)$#ism', $row['Type'], $matches);
    $enum = str_getcsv($matches[1], ",", "'");
    return $enum;
}

궁극적으로 "enum ()"과 분리 될 때 열거 형 값은 CSV 문자열이므로 그대로 구문 분석하십시오!


여기 mysqli입니다

function get_enum_values($mysqli, $table, $field )
{
    $type = $mysqli->query("SHOW COLUMNS FROM {$table} WHERE Field = '{$field}'")->fetch_array(MYSQLI_ASSOC)['Type'];
    preg_match("/^enum\(\'(.*)\'\)$/", $type, $matches);
    $enum = explode("','", $matches[1]);
    return $enum;
}
$deltypevals = get_enum_values($mysqli, 'orders', 'deltype');
var_dump ($deltypevals);

다음 과 같이 쿼리 할 때 jasonbar가 말하는 내용을 추가하고 싶습니다.

SHOW columns FROM table

결과를 배열로 얻으면 다음과 같이 보일 것입니다.

array([0],[Field],[1],[Type],[2],[Null],[3],[Key],[4],[Default],[5],[Extra])

여기서 [n]과 [text]는 동일한 값을 제공합니다.
내가 찾은 문서에서 실제로 말하지 않았습니다. 다른 것이 무엇인지 아는 것만으로도 좋습니다.


$row = db_fetch_object($result);
     if($row){
     $type = $row->Type;
     preg_match_all("/'([^']+)'/", $type, $matches,PREG_PATTERN_ORDER );
     return $matches[1];


}

이 시도

describe table columnname

해당 테이블의 해당 열에 대한 모든 정보를 제공합니다.


다음은 Laravel 프레임 워크에 적용한 Patrick Savalle이 제공 한 동일한 기능입니다.

function get_enum_values($table, $field)
{

   $test=DB::select(DB::raw("show columns from {$table} where field = '{$field}'"));

   preg_match('/^enum\((.*)\)$/', $test[0]->Type, $matches);
   foreach( explode(',', $matches[1]) as $value )
   {
       $enum[] = trim( $value, "'" );   
   }

   return $enum;

}

일부 모델의 방법으로 Codeigniter 적응 버전 :

public function enum_values($table_name, $field_name)
{
    $query = $this->db->query("SHOW COLUMNS FROM `{$table_name}` LIKE '{$field_name}'");

    if(!$query->num_rows()) return array();
    preg_match_all('~\'([^\']*)\'~', $query->row('Type'), $matches);

    return $matches[1];
}

결과:

array(2) {
    [0]=> string(13) "administrator"
    [1]=> string(8) "customer"
}

You can use this syntax for get enum possible values in MySQL QUERY :

$syntax = "SELECT COLUMN_TYPY FROM information_schema.`COLUMNS` 
WHERE TABLE_NAME = '{$THE_TABLE_NAME}' 
AND COLUMN_NAME = '{$THE_COLUMN_OF_TABLE}'";

and you get value, example : enum('Male','Female')

this is example sytax php:

<?php
function ($table,$colm){

// mysql query.
$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.
 $array_string = mysql_fetch_array($syntax);

    //Remove part string
    $string = str_replace("'", "", $array_string['COLUMN_TYPE']);
    $string = str_replace(')', "", $string);
    $string = explode(",",substr(5,$string));
}else{
    $string = "error mysql :".mysql_error();
}
// Values is (Examples) Male,Female,Other
return $string;
}
?>

For Laravel this worked:

$result = DB::select("SHOW COLUMNS FROM `table_name` LIKE 'status';");
$regex = "/'(.*?)'/";
preg_match_all( $regex , $result[0]->Type, $enum_array );
$enum_fields = $enum_array[1];
echo "<pre>";
print_r($enum_fields);

Output:

Array
(
[0] => Requested
[1] => Call Back
[2] => Busy
[3] => Not Reachable
[4] => Not Responding
)

The problem with every other answer in this thread is that none of them properly parse all special cases of the strings within the enum.

The biggest special case character that was throwing me for a loop was single quotes, as they are encoded themselves as 2 single quotes together! So, for example, an enum with the value 'a' is encoded as enum('''a'''). Horrible, right?

Well, the solution is to use MySQL to parse the data for you!

Since everyone else is using PHP in this thread, that is what I will use. Following is the full code. I will explain it after. The parameter $FullEnumString will hold the entire enum string, extracted from whatever method you want to use from all the other answers. RunQuery() and FetchRow() (non associative) are stand ins for your favorite DB access methods.

function GetDataFromEnum($FullEnumString)
{
    if(!preg_match('/^enum\((.*)\)$/iD', $FullEnumString, $Matches))
        return null;
    return FetchRow(RunQuery('SELECT '.$Matches[1]));
}

preg_match('/^enum\((.*)\)$/iD', $FullEnumString, $Matches) confirms that the enum value matches what we expect, which is to say, "enum(".$STUFF.")" (with nothing before or after). If the preg_match fails, NULL is returned.

This preg_match also stores the list of strings, escaped in weird SQL syntax, in $Matches[1]. So next, we want to be able to get the real data out of that. So you just run "SELECT ".$Matches[1], and you have a full list of the strings in your first record!

So just pull out that record with a FetchRow(RunQuery(...)) and you’re done.

If you wanted to do this entire thing in SQL, you could use the following

SET @TableName='your_table_name', @ColName='your_col_name';
SET @Q=(SELECT CONCAT('SELECT ', (SELECT SUBSTR(COLUMN_TYPE, 6, LENGTH(COLUMN_TYPE)-6) FROM information_schema.COLUMNS WHERE TABLE_NAME=@TableName AND COLUMN_NAME=@ColName)));
PREPARE stmt FROM @Q;
EXECUTE stmt;

P.S. To preempt anyone from saying something about it, no, I do not believe this method can lead to SQL injection.


All of you use some strange and complex regex patterns x)

Here's my solution without preg_match :

function getEnumTypes($table, $field) {
    $query = $this->db->prepare("SHOW COLUMNS FROM $table WHERE Field = ?");
    try {$query->execute(array($field));} catch (Exception $e) {error_log($e->getMessage());}
    $types = $query->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE, 1)[$field];
    return explode("','", trim($types, "enum()'"));
}

this will work for me:

SELECT REPLACE(SUBSTRING(COLUMN_TYPE,6,(LENGTH(COLUMN_TYPE)-6)),"'","")
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA='__TABLE_SCHEMA__' 
AND TABLE_NAME='__TABLE_NAME__'
AND COLUMN_NAME='__COLUMN_NAME__'

and then

explode(',', $data)

To fetch the list of possible values has been well documented, but expanding on another answer that returned the values in parenthesis, I wanted to strip them out leaving me with a comma separated list that would then allow me to use an explode type function whenever I needed to get an array.

SELECT
    SUBSTRING(COLUMN_TYPE, 6, LENGTH(COLUMN_TYPE) - 6) AS val
FROM
    information_schema.COLUMNS
WHERE
    TABLE_NAME = 'articles'
AND
    COLUMN_NAME = 'status'

The SUBSTRING now starts at the 6th character and uses a length which is 6 characters shorter than the total, removing the trailing parenthesis.


It is extraordinary how none of you has thought that if you are using an enum field it means that the values to be assigned are known "a priori".

Therefore if the values are known "a priori" the best ways to manage them is through a very simple Enum class.

Kiss rule and save one database call.

<?php
class Genre extends \SplEnum {
 const male = "Male";
 const female = "Female";
}

http://it2.php.net/manual/en/class.splenum.php


I get enum values in this way:

SELECT COLUMN_TYPE 
FROM information_schema.`COLUMNS` 
WHERE TABLE_NAME = 'tableName' 
     AND COLUMN_NAME = 'columnName';

Running this sql I have get : enum('BDBL','AB Bank')

then I have filtered just value using following code :

preg_match("/^enum\(\'(.*)\'\)$/", $type, $matches);
$enum = explode("','", $matches[1]);
var_dump($enum) ;

Out put :

array(2) { [0]=> string(4) "BDBL" [1]=> string(7) "AB Bank" }


For PHP 5.6+

$mysqli = new mysqli("example.com","username","password","database");
$result = $mysqli->query("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='table_name' AND COLUMN_NAME='column_name'");
$row = $result->fetch_assoc();
var_dump($row);

DELIMITER //

    DROP FUNCTION IF EXISTS ENUM_VALUES;

    CREATE FUNCTION ENUM_VALUES(

        _table_name VARCHAR(64), 
        _col_name VARCHAR(64)

    ) RETURNS JSON

        BEGIN

            RETURN (
                SELECT CAST(CONCAT('[', REPLACE(SUBSTRING(COLUMN_TYPE, 6, LENGTH(COLUMN_TYPE) - 6), "'", '"'), ']') AS JSON)
                  FROM information_schema.COLUMNS
                 WHERE TABLE_SCHEMA = 'db_name'
                   AND TABLE_NAME   = _table_name
                   AND COLUMN_NAME  = _col_name
                   AND DATA_TYPE    = 'enum'
            );

        END //

DELIMITER ;

Example:

SELECT ENUM_VALUES('table_name', 'col_name');

참고URL : https://stackoverflow.com/questions/2350052/how-can-i-get-enum-possible-values-in-a-mysql-database

반응형