当前位置:网站首页>How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function
How to query fields separated by commas in MySQL as query criteria - find_ in_ Set() function
2022-06-23 06:11:00 【Bald and weak.】
List of articles
Write it at the front
Use mysql when , It is possible that a field represents a set , If this set is drawn into a single table, it is not worth it , At this time, we store , You can choose to separate the data with commas ( You can only use commas in English ), As shown in the figure :
How to check when making a query ?
As query criteria
If you give a data as a query condition , Determine whether the field exists , How should I check it ?
SELECT * FROM student where find_in_set(' Sing a song ', sign) > 0;
Use find_in_set() Functions are easy to implement , take sign The field contains ’ Sing a song ’ Attribute data , Rather than using like.
Multi conditional queries are used for mybatis
If you want to query multiple conditions , For example, both Sing a song 、 Again in line with dance Of , You can write like this :
<if test="sign != null and sign != ''">
<foreach item="item" index="index" collection="sign.split(',')">
AND find_in_set(#{item} , sign) > 0
</foreach>
</if>
Aggregate query count total
How to calculate the total ?
SELECT sum(LENGTH(sign) - LENGTH(REPLACE(sign,',','')) + 1) count FROM student;

The length of the original field contents - The length of the contents after deleting the comma = How many commas are in this field ;
However, the last one is without commas, so +1
Inquire about distinct A list of
There is no better way , Only first distinct sign This field , Find out , Then use the program to judge one by one ...
边栏推荐
- Matplotlib savefig multiple picture overlay
- Gplearn appears assignment destination is read only
- ant使用总结(二):相关命令说明
- SQL statement error caused by the same SQL table name and function name.
- [database backup] complete the backup of MySQL database through scheduled tasks
- [open source project] excel export Lua configuration table tool
- Efficient office of fintech (I): automatic generation of trust plan specification
- WordPress contact form entries cross cross site scripting attack
- 内存分析与内存泄漏检测
- Pat class B 1023 minimum decimals
猜你喜欢

三项最高级认证,两项创新技术、两大优秀案例,阿里云亮相云原生产业大会

jvm-01. Instruction rearrangement

True MySQL interview question (XXII) -- condition screening and grouping screening after table connection

ant使用总结(一):使用ant自动打包apk

如何指定pig-register项目日志的输出路径

Addressing and addressing units

ant使用总结(三):批量打包apk

Activity startup mode and life cycle measurement results

Adnroid activity截屏 保存显示到相册 View显示图片 动画消失

android Handler内存泄露 kotlin内存泄露处理
随机推荐
Visual studio debugging tips
Radar canvas
【DaVinci Developer专题】-42-如何生成APP SWC的Template和Header文件
SQL statement error caused by the same SQL table name and function name.
如何指定pig-register项目日志的输出路径
JS interview question - anti shake function
Layer 2技术方案进展情况
android Handler内存泄露 kotlin内存泄露处理
Add and multiply two polynomials using linked list
Real MySQL interview questions (25) -- common group comparison scenarios
Real MySQL interview question (23) -- pinduoduo ball game analysis
Memory analysis and memory leak detection
Kotlin android简单Activity跳转、handler和thread简单配合使用
Palindrome number for leetcode topic analysis
Sorting out common problems after crawler deployment
【Cocos2d-x】截图分享功能
Simple about fastdfs
Pat class B 1020 Moon Cake
Pat class B 1022 d-ary a+b
【Cocos2d-x】可擦除的Layer:ErasableLayer