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%'

Menu