在MySQL中返回枚举值的最佳方法是什么? [英] What is the best way to return enum values in MySQL?

查看:86
本文介绍了在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';

和另一个:

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

尽管第一个查询将为我提供此信息:

Although the first query will give me this info:

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

第二个查询为我提供了相同的内容,并带有其他列.我宁愿只获取那些没有 enum()和逗号的值,这是可能的,还是我需要解析这些值?并不是仅检查是否有更简单的方法就很困难.

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?

推荐答案

我想您无法选择这些值,我最终用以下方法解析出这些值:

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;

这篇关于在MySQL中返回枚举值的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆