如何在MySql中选择ENUM类型的值个数?
我需要从下表中选择一行,但问题是 $row['city']
中的值是该值的文本表示,而我需要它的编号(Toronto = 2)。 (与我们 INSERT INTO 时相同,我们使用值数字而不是文本)
请求表结构:
req_id INT
uname VARCHAR(30)
city ENUM('New York', 'Toronto', 'Las Vegas')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需要从 city 转换为数字上下文“nofollow noreferrer”>精美手册:
所以您想要这样的事情:
顺便说一句,您可以使用字符串或整数插入
enum
表示。You just need to force your
city
into a numeric context, from the fine manual:So you want this sort of thing:
BTW, you can insert an
enum
using either the string or integer representation.您可以使用
CAST
函数。 文档没有提到这个特定的用例,但它按预期工作。我更喜欢它,因为它看起来优雅而清晰:You can use the
CAST
function. The documentation doesn't mention this specific use case, but it works as expected. I prefer it because it looks elegant and clear: