How to optimize a Mysql search?

There is a need to check the database for the presence of pairs of records field1-field2. It is supposed to go search and Vice versa field2-field1. Plus, each field has the characteristics char1, char2, which must be the same. So saw this design search:

$q = "SELECT * FROM `table` WHERE (`field1`='".trim($a1[0])."' && `char1`='".trim($a1[1])."' && `field2`='".trim($a2[0])."' && `char2`='".trim($a2[1])."') OR (`field1`='".trim($a2[0])."' AND `char1`='".trim($a2[1])."' AND `field2`='".trim($a1[0])."' AND `char2`='".trim($a1[1])."')";

Is it possible to shorten the query, i.e., apply some kind of statement or what?
June 14th 19 at 19:09
1 answer
June 14th 19 at 19:11
Is there a way to optimize the query, i.e., apply some kind of statement or what?

But there is a performance problem? how the query is executed, what is the volume of the base?
Speed did not measure, I just the query itself does not like. I would like it more beautiful to do. And how it works. - Haley_Haag93 commented on June 14th 19 at 19:14
just looks crooked due to the curve code perepeshite under pdo and norms )
But in General - if it works -no need to pull, the task code to work, not look pretty. And in General - you could make the variables before you insert it into the query - it would not be so dumb, and pdo all will be beautiful.

$sql = "SELECT * FROM `table` 
WHERE (
`field1`=:field1 and 
`char1`=:char1 and 
`field2`=:field2 and 
`char2`=:char2
) 
OR (
`field1`=:field12 and 
`char1`=:char12 and 
`field2`=:field22 and 
`char2`=:char22
)";
- lily.Kozey commented on June 14th 19 at 19:17
,

I will try. PDO has not yet been taken. mysqli use - Haley_Haag93 commented on June 14th 19 at 19:20

Find more questions by tags MySQLPHP