简体   繁体   English

如何在mysql中将一个查询结果减去另一个查询

[英]how to subtract one query result to another query in mysql

this is my first query 这是我的第一个查询

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'

this is my second query 这是我的第二个问题

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 this is my third query 这是我的第三个问题

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 after that, I am merging the second and third query result 之后,我正在合并第二个和第三个查询结果

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

so my question is that if I want to subtract query one result to merge result means with the (query 2 and 3)ie in my case: $ids_reactivated 所以我的问题是,如果我想减去查询一个结果,合并结果意味着(查询2和3),即在我的情况下:$ ids_reactivated

anyone have an idea how to do it ... I already tried many ways and passed one day... 任何人都知道怎么做...我已经尝试了很多方法并且有一天过去了...

hope pepls help me thanks 希望pepls帮助我谢谢

thanks for your hint but i got my answer like this ... 谢谢你的暗示,但我得到了这样的回答......

   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' 
    **and u.nickname not in ($query2)**

UNION 
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' 
    **and u.nickname not in ($query3)**

Its simple You can make use of union for merging and not in for substraction Following is the sample 它很简单你可以使用union来合并而不是用于减法以下是样本

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)**

change the $query1 with your query ,it should give the result 使用您的查询更改$ query1,它应该给出结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM