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

Categories

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

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

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

谢谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

我做的话思路大概是这样

如果有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...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...