For example, there are 2 fields in the database - code1, code2. Can be from 1 to 3 characters. I have a composite code, i.e. code1 + code2. And I cannot recover 2 codes from this line, i.e. I need to look for something like that -

WHERE (`code1``code2`) = $code1and2 

How it's done? And if anyone knows how to do this using ActiveRecord Yii, it will be awesome.

  • one
    In mysql, strings are combined using the concat function, i.e. where concat(code1, code2)=... - Mike
  • Thank you, I did not think that they can combine properties. - Oleg Grigoriev

1 answer 1

MySQL uses the CONCAT function to concatenate strings.

CONCAT(str1,str2,...)

But there is one thing - if you use expressions that perform operations on records in the WHERE condition, then mysql will have to calculate this expression for each record in the table, and then only filter. The index will not be used in this case, and with a large number of records in the table, such a query may work very slowly.

It is better to somehow divide the line by which you want to filter the records, and search for each field separately. Or, add a field containing the full line and create an index for it.