Let us understand how to search two keys in MySQL
With help you can use “OR” to achieve Search for a well-optimized single key or use a well-optimized "AND". Let’s see how to combine two different keys with the “OR” operation to search -
SELECT field1_index, field2_index FROM tableName WHERE field1_index = '1' OR field2_index = '1'
This is the optimized version of the query. This can also be done efficiently using a "UNION" which combines the output of two separate "SELECT" statements. Each "SELECT" statement searches only one key and can be optimized. Let’s see the actual query -
SELECT field1_index, field2_index FROM tableName WHERE field1_index = '1' UNION SELECT field1_index, field2_index FROM tableName WHERE field2_index = '1';
The above is the detailed content of Search two keys in MySQL. For more information, please follow other related articles on the PHP Chinese website!