find_in_set - 请教 mysql查询
ringa_lee
ringa_lee 2017-04-17 14:57:45
0
8
734

mysql 表中字段option_tag 存储的值格式(分号)为: 4:539;8:543;4:545;8:549
通过 find_in_set() 可以查找到以逗号分隔的字符串。
以分号分隔的 字符串 有什么函数直接能查询到 option 含有 539的所有值?

ringa_lee
ringa_lee

ringa_lee

reply all(8)
洪涛

sql statement:
SELECT * FROM table
WHERE locate(':539;', CONCAT(options_tag, ';')) > 0

The logic of using like is not rigorous. For example, 4:2539 will also be matched by like, but it is not the desired result.

From the design perspective, if it is a complex data type, it is recommended to save it in json format. The higher version of mysql directly has built-in json parsing and query functions.

巴扎黑

Just use like or regular rules.
However, it is not recommended to put logic in mysql, which will cause excessive pressure on the database engine and is extremely unsafe.
The correct approach should be to take out the value of the entire field, use it as a string and then use a language such as php python nodejs to process it.

洪涛
SELECT * FROM table
WHERE options_tag LIKE '%539%';

As @小草哥 said, this is a design flaw. This seems to be a one-to-many or many-to-many relationship. It should not be stored in a field. Not only is the query efficiency low (the field is processed in SQL The operation may make the index of this field unusable), and the query is not flexible.

阿神

like match

Ty80

like :539

洪涛

I say this is a design flaw, does anyone agree?

Ty80

like similar matching

PHPzhong

Blurry
Query
Research

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!