How does mysql Query data that matches letters in a field based on letters in a string? for example:
A string may be A, B, C
It may also be B, C
Or C
The fID field in the data table contains multiple A, B, or C
If the characters contain A, B, C, you need to query the data whose fID is A, B, or C.
If the characters B and C are included, you must query the data whose fID is B or C.
Create table 'tab '(
'Fid' VARCHAR (2) null default null,
'Fname' VARCHAR (10) NULL
)
COLLATE = 'utf8 _ general_ci'
ENGINE = InnoDB;
Insert into 'tab' ('fid', 'fname') VALUES
('A', 'a01 '),
('A', 'a02 '),
('A', 'a03 '),
('A', 'a04 '),
('B', 'b02 '),
('B', 'b03 '),
('B', 'b04 '),
('B', 'b01 '),
('B', 'b05 '),
('C', '02 '),
('C', 'c03 '),
('C', 'c01 ');
Reply to discussion (solution)
Assume that the string you entered is in the format of (or after processing) A, C, and B.
Strict match
Select * from tab where find_in_set (fID, '$ P1 ')
Fuzzy match
Select * from tab where '$ raise' like concat ('%', fID, '% ')
Thanks for the xuzuning solution, and solved it with find_in_set (fID, '$ P1').