3

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)i.e in my case: $ids_reactivated

anyone have an idea how to do it ... I already tried many ways and passed one day...

hope pepls help me thanks

JYOTI SAPARIYA
  • 384
  • 2
  • 13
  • https://stackoverflow.com/questions/1589070/subtraction-between-two-sql-queries – Gernot Aug 13 '18 at 17:09
  • 1
    Possible duplicate of [Subtraction between two sql queries](https://stackoverflow.com/questions/1589070/subtraction-between-two-sql-queries) – Gernot Aug 13 '18 at 17:09

2 Answers2

3

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)**
JYOTI SAPARIYA
  • 384
  • 2
  • 13
1

Its simple You can make use of union for merging and not in for substraction Following is the sample

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

Mahesh Hegde
  • 1,131
  • 10
  • 12