gpt4 book ai didi

mysql - 当我的列数据类型为 SET 时,我需要使用什么类型的 SQL 查询?

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:01 24 4
gpt4 key购买 nike

MYSQL 表 my_contacts

contact_id INT(11) AUTO_INCREMENT,
first_name VARCHAR(255),
interests SET('Animals','Trading Cards','Geocaching','Horseback Riding','Movies','Hiking');

SQL 查询

SELECT * FROM my_contacts WHERE interests = 'Animals';

示例

如果我的表中有一条关于兴趣 Animals 的记录,上面的查询就可以正常工作。 ;-)

但是,如果我的表中有一条记录对AnimalsHiking 感兴趣,则上面的查询不会返回任何结果。 :-(

为什么?

最佳答案

使用FIND_IN_SET(str,strlist) :

SELECT * FROM my_contacts WHERE FIND_IN_SET('Animals', interests) > 0;

有关 SET 类型和操作的更多信息:http://dev.mysql.com/doc/refman/5.7/en/set.html

关于mysql - 当我的列数据类型为 SET 时,我需要使用什么类型的 SQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com