1. In MySQL 8, when the IN condition is used on multiple fields, the query result is empty. See Figure 1.

SELECT
`id`,
`name`,
`email`,
`type`
FROM
`customers`
WHERE
( 'name,email' ) IN ( '(\'客户姓名二\',\'44445@163.com\'),(\'客户姓名\',\'44445@163.com\')' )
ORDER BY
`id` DESC
2. Reference: Using the IN condition on multiple fields in MySQL 8 The SQL that returns a non-empty query result is as follows:
SELECT * FROM `customers` WHERE ( NAME, email ) IN (( '123', '123@outlook.com' ),
( '我是客户姓名33', '1303842899@qq.com' ))
3. Reference: When using raw expressions for queries in Laravel 9, an error occurs: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; I adjusted the code and printed the bound parameters, which matched expectations without any issues. See Figure 2.
![Reference: When using raw expressions for queries in Laravel 9, an error occurs: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;](https://media.shuijingwanwq.com/2024/06/2.png)
$str = '';
foreach ($criteria['whereRawIn'][1] as $key => $customer) {
$str .= ($key == 0) ? '(\'' : ',(\'';
$str .= implode('\',\'', $customer) . '\')';
}
var_dump($criteria['whereRawIn'][0]);
var_dump($str);
exit;
string(10) "name,email"
string(2959) "('客户姓名二','44445@163.com'),('客户姓名','44445@163.com')"
4. The adjusted code implementation is as follows:
$str = '';
foreach ($criteria['whereRawIn'][1] as $key => $customer) {
$str .= ($key == 0) ? '(\'' : ',(\'';
$str .= implode('\',\'', $customer) . '\')';
}
// var_dump('(' . $criteria['whereRawIn'][0] . ')');
// var_dump('(' . $str . ')');
// exit;
$builder->whereRaw('? in ?', ['(' . $criteria['whereRawIn'][0] . ')', '(' . $str . ')']);
5. At runtime, an error occurs: 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 ‘? order by `id` desc’ at line 1. See Figure 3.
![At runtime, an error occurs: 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 '? order by `id` desc' at line 1.](https://media.shuijingwanwq.com/2024/06/3.png)
6. However, copying the error-reporting SQL into the database and executing it manually succeeds. See Figure 4.

7. The adjusted code implementation is as follows. At runtime, an error occurs: SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 2 column(s). However, copying the error-reporting SQL into the database and executing it manually succeeds. See Figure 5.
![The adjusted code implementation is as follows. At runtime, an error occurs: SQLSTATE[21000]: Cardinality violation: 1241 Operand should contain 2 column(s). However, copying the error-reporting SQL into the database and executing it manually succeeds](https://media.shuijingwanwq.com/2024/06/5.png)
$str = '';
foreach ($criteria['whereRawIn'][1] as $key => $customer) {
$str .= ($key == 0) ? '(\'' : ',(\'';
$str .= implode('\',\'', $customer) . '\')';
}
// var_dump('(' . $criteria['whereRawIn'][0] . ')');
// var_dump('(' . $str . ')');
// exit;
// $builder->whereRaw('? in ?', ['(' . $criteria['whereRawIn'][0] . ')', '(' . $str . ')']);
$builder->whereRaw('(?,?) in (?)', ['name', 'email', $str]);
// $builder->whereRaw('( ' . $criteria['whereRawIn'][0] . ' ) in (?)', [$str]);
8. I ultimately decided to abandon this SQL and switch to a different SQL format. The code implementation and generated SQL are as follows. The program no longer reports errors at runtime, and the query result meets expectations. See Figure 6.

foreach ($criteria['names_emails'] as $customer) {
$builder->orWhere(function ($query) use ($customer) {
$query->where('name', $customer[0])
->where('email', $customer[1]);
});
}
SELECT
`id`,
`name`,
`email`,
`type`
FROM
`customers`
WHERE
( `name` = '客户姓名二' AND `email` = '客户姓名二' )
OR ( `name` = '客户姓名' AND `email` = '客户姓名' )
ORDER BY
`id` DESC
