微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php-如何将一个查询结果减去mysql中的另一个查询

这是我的第一个查询

SELECT disTINCT(u.nickname) FROM user u 
                           where   u.id  IN(SELECT `submission_user`.`user_id` from `submission_user`) AND
                           u.member_since   >= '2015-07-01'

这是我的第二个查询

SELECT disTINCT(u.nickname) FROM user u LEFT JOIN submission_user su ON su.user_id = u.id  
                            LEFT JOIN submission s ON s.id = su.submission_id
                             WHERE  s.date_time BETWEEN '2017-10-31' and '2018-07-31'

这是我的第三个查询

SELECT disTINCT(u.nickname) FROM user u LEFT JOIN submission_user su ON su.user_id = u.id  
                            LEFT JOIN track_user tu ON tu.user_id = u.id
                            LEFT JOIN track ON track.id = tu.track_id                       
                            where track.uploaded_timestamp BETWEEN '2017-10-31' and '2018-07-31'

然后,我将第二和第三查询结果合并

 $ids_reactivated = array_unique(array_merge($track_user, $submit_user));

所以我的问题是,如果我想减去查询一个结果以将结果均值与(查询2和3)合并,即我的情况:$ids_reactivated

任何人都有一个想法怎么做…
我已经尝试了许多方法,并度过了一天…

希望人民帮助我谢谢

解决方法:

它很简单,您可以利用并集进行合并,而不要进行减法
以下是示例

SELECT disTINCT(u.nickname) FROM user u LEFT JOIN submission_user su ON su.user_id = u.id LEFT JOIN submission s ON s.id = su.submission_id
WHERE  s.date_time BETWEEN '2017-10-31' and '2018-07-31' 
**and u.nickname not in ($query1)**

UNION

SELECT disTINCT(u.nickname) FROM user u LEFT JOIN submission_user su ON su.user_id = u.id LEFT JOIN track_user tu ON tu.user_id = u.id LEFT JOIN track ON track.id = tu.track_id 
where track.uploaded_timestamp BETWEEN '2017-10-31' and '2018-07-31' 
**and u.nickname not in ($query1)**

查询更改$query1,它应该给出结果

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐