I want to export some data from the DB.
Basically what I want to say is this:
1- Select mbr_name
from the members
table
2- Choose the ones that exist at the course_registration
table (based on mbr_id
)
3- Join the course_registration
ids with course_comments
table
Then I need to apply these WHERE condtions as well:
1- Make sure that crr_status
at course_registration
table is set to completed
2- Make sure that crr_ts
at course_registration
table is between "2021-03-07 00:00:00"
AND "2022-03-17 00:00:00"
3- Make sure that crm_confirmation
from course_comments
table is set to accept
So I tried my best and wrote this:
SELECT members.mbr_name
FROM members
INNER JOIN course_registration AS udt ON members.mbr_id = udt.crr_mbr_id
INNER JOIN course_comments AS dot ON udt.crr_cor_id = dot.crm_reference_id
WHERE udt.crr_status = "completed" AND udt.crr_ts >= "2021-03-07 00:00:00" AND udt.crr_ts < "2022-03-17 00:00:00"
AND dot.crm_confirmation = "accept";
But this will give wrong data somehow.
The actual number of members
that have all these conditions are 12K but this query gives me 120K results which is obviously wrong!
So what's going wrong here? How can I solve this issue?
UPDATE:
Here are the keys of each table:
members (mbr_id (PK), mbr_name)
course_registration (crr_id (PK), crr_mbr_id (FK), crr_cor_id (FK), crr_status)
course_comments (crm_id (PK), crm_reference_id (FK), crm_confirmation)