Home > Software engineering >  How mysql query multiple to specify comma-separated values in a comma-separated list of data
How mysql query multiple to specify comma-separated values in a comma-separated list of data

Time:09-20

How mysql query multiple to specify comma-separated values in a comma-separated list of data
There is a table of table1, for example, there is a bar called tag, the tag data for
The tag
-
34, 2
10
10 34
10, 2
10,34,2
Query conditions the tag only request out when he is 34, 10 second (10) and article 3 (34) 10 records,
The tag to 10, 2 only called for the query of article 2 (10) and (1, 2) of article 4 of the records,

SELECT * FROM table1 the WHERE (FIND_IN_SET (34, tag) OR FIND_IN_SET (10, the tag))

SELECT * FROM table1 the WHERE the tag IN (34, 10)

LOCATE the REGEXP functions and methods are used, can't find a suitable solution
Thank you very much ~ ~ ~

CodePudding user response:

The following for the test data:

CREATE TABLE test (id int (6) the NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), the tag VARCHAR (50), NOT NULL).

And then inserted into the test data with commas
INSERT INTO test (tag) VALUES (' 34, 2 ');
INSERT INTO test (tag) VALUES (' 10 ');
INSERT INTO test (tag) VALUES (' 10, 34);
INSERT INTO test (tag) VALUES (' 10, 2 ');
INSERT INTO test (tag) VALUES (' 10,34,2 ');

CodePudding user response:

Such a complex logic program to achieve convenient, SQL statements to estimate difficulties,

CodePudding user response:

In mysql write a function

CodePudding user response:

Your field in the data is not the only, so the judge division and comparison, need to be solved only by the query is not completed, can be accomplished by means of query + function, write a custom function, will query the judgment of the conditions change,

CodePudding user response:

Circular array [1, 2, 3], joining together the SQL is this:


Ok, meet the demand, a fix

CodePudding user response:

This requirement, don't you like?

The tag is a string, then use the like search it
  • Related