Ask how to write a SQL.

the form of records saved in type2 in field An of Table A: 1, br 3, 5, 7,

I want to find a record containing 2 in the type2 field. How do I write SQL?

Apr.02,2021

No type2 like "% 2" directly, you can try:

SELECT "22,33" LIKE "%2%";

the result is not 0.

need to use FIND_IN_SET:

where FIND_IN_SET('2', type2) > 0;

like is a wide range of fuzzy matches, with no delimiter in the string, find_in_set is an exact match, and field values are in English "," delimited


SELECT * FROM A where find_in_set (type2,2);


several solutions

  1. Like
  2. Find_in_set
  3. regexp

4. Quote es, solr and other full-text search

if the data is large, the first three are not recommended


select * from table_name t where t.type2 like'% 2%'

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e462bf-438ca.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e462bf-438ca.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?