1. Reference: Using the IN condition for multiple fields in MySQL 8.
SELECT * FROM `customers` WHERE ( name, email ) IN (( '123', '123@outlook.com' ),
( '我是客户姓名33', '1303842899@qq.com' ))
2. I needed to use the IN condition for multiple fields when using raw expression queries in Laravel 9. The whereRaw and orWhereRaw methods inject a raw “where” clause into your query.
$customers = DB::table('customers')
->whereRaw('email in ?', ['(\'123@outlook.com\', \'12414dfgfdg@78.com\')'])
->get();
print_r($customers);exit;
3. Execution error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘?’ at line 1. See Figure 1.
![Execution error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1](https://media.shuijingwanwq.com/2024/03/1-4.png)
"message": "SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?' at line 1 (SQL: select * from `customers` where email in ('123@outlook.com', '12414dfgfdg@78.com'))",
4. Instead of using ?, I directly concatenated the raw SQL. The query succeeded. See Figure 2.

$customers = DB::table('customers')
->whereRaw('email in (\'123@outlook.com\', \'12414dfgfdg@78.com\')')
->get();
print_r($customers);exit;
5. Adjusted again to apply the IN condition to multiple fields. The query succeeded. See Figure 3.

$queryBuilder = app('Modules\Order\Models\Customer')::query();
$customers = $builder->whereRaw('(name,email) in ((\'123\',\'123@outlook.com\'),(\'我是客户姓名33\',\'1303842899@qq.com\'))')
->get();
print_r($customers);exit;
$queryBuilder = app('Modules\Order\Models\Customer')::query();
print_r($criteria['customers']);
$str = '';
foreach ($criteria['customers'] as $key => $customer) {
$str .= ($key == 0) ? '(\'' : ',(\'';
$str .= implode('\',\'', $customer) . '\')';
}
print_r($str);
$customers = $builder->whereRaw('(name,email) in (' . $str . ')')->get();
print_r($customers);exit;
6. Checked the generated SQL, which matched expectations. See Figure 4.

select
`id`,
`name`,
`email`,
`type`
from
`customers`
where
(name, email) in (
('王某人', '44445@163.com'),
('客户姓名', '44445@163.com'),
('王某某', '4444@163.com'),
('王某某', '44445@163.com'),
('李某某', '4444@163.com'),
('客户姓名', '4444@163.com')
)
order by
`id` desc
7. Later, I encountered an SQL error. For details, see: Error when using raw expression queries in Laravel 9: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;.
