SMILET
我没测试过性能。
不过如果打算不进行联表查询的话,就通过 php 语句来处理:
比如有表 DDL 语句:
create table A (
id int primary key auto_increment not null ,
name varchar(255) comment '名称' ,
course_id int comment '对应B.id' ,
);
create table B (
id int primary key auto_increment not null ,
course varchar(255) comment '课程名称'
);
要求:符号 A.id in (1 , 2 , 3) 并且 A.course_id 对应 B 表中的 course 字段包含 test 值的所有记录。
SQL 联表查询:
select * from A inner join B on a.course_id = B.id where A.id in (1 , 2 , 3) and B.course like '%test%';
PHP 查询:
$sql = "select * from A where id in (1 , 2 , 3)";
$data = \DB::query($sql);
$res = [];
foreach ($data as $v)
{
$sql = "select * from B where id = {$v} and course like '%test%'";
$part = \DB::query($sql);
$res = array_merge($v , $part);
}
// 符合条件的结果
print_r($res);