Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

tp5 数据库查询 如何进行多对多查询

我邀请的会员(在会员表)有多个,邀请的会员会去做任务(任务表)

想查询一下我邀请的所有会员 完成的单数 这样的sql 怎么去写才是更优化的。~~~~

我当前想到的就是直接先查会员 然后用foreach 再一个个的去查

谢谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

我做的话思路大概是这样

如果有redis就从redis查数据,不走mysql

// 查询我所邀请的会员, 并获取id
$aMember    = db('会员表')->where($where)->field($field)->select();
$aMemberIds = array_column($aMember, 'member_id');

// 查询会员id对应的任务
$aTask = db('任务表')->where(['member_id' => ['in', $aMemberIds]])->field($field)->select();

// 处理两个数组
foreach ($variable as $key => $value) {
    # code...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...