Home > database >  Mysql how to judge the field contains a string to return 1 if true, is false returns 0
Mysql how to judge the field contains a string to return 1 if true, is false returns 0

Time:12-19

If inscribe
This judgment as long as it is used for substring_index (),
 
Select
Substring_index (a_name, '|', 1) as m
Substring_index (a_name, '|', 1) as n
The from
TableA

So write, if can't find '|' m and n are returned is a_name
Expectations are m return a_name, n return is empty (" "),
Think of way is to add a judgment, but can't try for a long time, come to a big,

CodePudding user response:

Should use INSTR function, INSTR has two parameters, using the method about the following:
INSTR (A, B), if A consists of B, back in the position of A B, if A does not contain B, returns 0,
Sample:
SELECT INSTR (' 123 | ', '|'), INSTR (' 123 ', '|');
What you need is something like this:
SELECT the IF (INSTR (' 123 | ', '|')=0, 1), the IF (INSTR (' 123 ', '|')=0, 1);
Or, the following three lines to perform together,
The SET @ A:='123 |';
SET @ : B='123';
SELECT the IF (INSTR (@ A, '|')=0, 1), the IF (INSTR (@ B, '|')=0, 1);

CodePudding user response:

Expectations are m return a_name, n return is empty (" ")
What is this requirement, whether to contain |,

CodePudding user response:

Select a case when a_name like '% % |' then one else a_name end
The from tableA
  • Related