Home  >  Article  >  Database  >  Search two keys in MySQL

Search two keys in MySQL

PHPz
PHPzforward
2023-09-13 17:25:02842browse

在 MySQL 中搜索两个键

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 -

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Install MySQL on FreeBSDNext article:Install MySQL on FreeBSD