当前位置:网站首页>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 ...
边栏推荐
- APP SHA1获取程序 百度地图 高德地图获取SHA1值的简单程序
- 如何指定pig-register项目日志的输出路径
- Eight data analysis models: ogsm model
- 给定二叉树的某个节点,返回该节点的后继节点
- Visual studio debugging tips
- WordPress Core 5.8.2 - 'WP_ Query'SQL injection
- Pit filling for abandoned openssl-1.0.2 (.A to.So)
- Kotlin android简单Activity跳转、handler和thread简单配合使用
- Palindrome number for leetcode topic analysis
- Basic calculator for leetcode topic analysis
猜你喜欢

Real MySQL interview questions (25) -- common group comparison scenarios

jvm-04.对象的内存布局

New classes are launched | 5 minutes each time, you can easily play with Alibaba cloud container service!

Wireshark TS | 视频 APP 无法播放问题

Real MySQL interview questions (XXVI) -- didi 2020 written examination questions

又到半年总结时,IT人只想躺平

Infotnews | which Postcard will you receive from the universe?

Cloud native database is the future

编址和编址单位

Memory analysis and memory leak detection
随机推荐
WordPress contact form entries cross cross site scripting attack
[cocos2d-x] erasable layer:erasablelayer
Deploy docker and install MySQL in centos7
ant使用总结(一):使用ant自动打包apk
Layer 2技术方案进展情况
Addressing and addressing units
Excel sheet column number for leetcode topic resolution
Visual studio debugging tips
PAT 乙等 1010 C语言
Pat class B 1013 C language
matplotlib savefig多个图片叠加问题
WordPress Core 5.8.2 - 'WP_ Query'SQL injection
Excel sheet column title for leetcode Title Resolution
Pat class B 1015 C language
【开源项目】excel导出lua配置表工具
机器学习3-岭回归,Lasso,变量选择技术
Explicability of counter attack based on optimal transmission theory
微软面试题:打印折纸的折痕
Pyinstaller package exe setting icon is not displayed
Pit filling for abandoned openssl-1.0.2 (.A to.So)