简体   繁体   中英

How to get opposite results of a specific sql query?

I have 3 tables which are divided in users, user_courses and course.

I wanted to create 2 queries:

  1. One query to return the subscribed courses of a specific user
  2. Another query to return the unsubscribes courses of a specific user

I got the first query done but I can´t make the 2nd query work.

I tried to negate the conditions and got the same rows multiple times.

SELECT 
    course.course_id,course_name,course_owner,course_date,course_time
FROM
    course, users, user_courses
WHERE 
    usershortcut = "mmuster" 
    AND user_courses.user_id = users.user_id 
    AND user_courses.course_id = course.course_id

This is the first query which gives out all the subscribed courses of a user and I have no clue how to reverse it so I get the unsub courses of that user.

If the query you mention gives you the subscribed courses, for the second one you can use a filtering condition as in:

select *
from course
where course_id not in (
  SELECT course.course_id
  FROM course,users,user_courses
  WHERE usershortcut="mmuster" and user_courses.user_id = users.user_id 
  and user_courses.course_id = course.course_id
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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