我有两个表A和表B。
表A具有 userid 和 course_id ,表B具有 userid 和 courseid 。
我想要一个没有表B中所有记录的表。它不应包含表B中的任何记录。
例如
如果表A具有:
userid: 224 courseid: 6
userid: 224 courseid: 7
userid: 224 courseid: 8
如果表B具有:
userid: 224 courseid: 6
userid: 224 courseid: 7
然后在新表中应具有:
userid: 224 courseid: 8
我对表A的代码:
SELECT ue.userid AS userid,
Concat(u.firstname, ' ', u.lastname) AS user,
en.courseid AS course_Id,
co.fullname AS Course_Name,
cu.companyid AS companyId,
company.name
FROM `mdl_user_enrolments` AS ue
LEFT JOIN `mdl_enrol` AS en
ON ue.enrolid = en.id
LEFT JOIN `mdl_course` AS co
ON en.courseid = co.id
LEFT JOIN `mdl_user` AS u
ON ue.userid = u.id
LEFT JOIN `mdl_company_users` AS cu
ON cu.userid = ue.userid
LEFT JOIN `mdl_company` AS company
ON company.id = cu.companyid
WHERE co.id IN ( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12 )
AND company.id = 1
表B:
SELECT u.id,
p.course
FROM mdl_course_completions AS p
JOIN mdl_course AS c
ON p.course = c.id
JOIN mdl_user AS u
ON p.userid = u.id
WHERE c.enablecompletion = 1
AND p.course IN ( 1, 2, 3, 4,
5, 6, 7, 8,
9, 10, 11, 12 )
输出:
我不要表B中的任何行,表B中的行与 userid 和 courseid 进行比较。
我尝试了此查询以获取所需的输出。但是它从表B中常见的表A中排除了所有用户(userid)。
选择ue.userid作为用户ID,CONCAT(u.firstname,'',u.lastname)作为 用户,en.courseid为Course_Id,co.fullname为Course_Name,cu.companyid 作为companyId,从
mdl_user_enrolments
开始,在ue左侧加入mdl_enrol
为en ue.enrolid = en.id左加入mdl_course
为co ON en.courseid = co.id以u身份左加入mdl_user
ON ue.userid = u.id 以cu ON身份左联接mdl_company_users
cu.userid = ue.userid左联接mdl_company
作为公司ON company.id = cu.companyid,其中co.id IN (1,2,3,4,5,6,7,8,9,10,11,12)AND company.id = 1 AND ue.userid NOT IN (从mdl_course_completions AS p JOIN mdl_course AS c ON中选择u.id p.course = c.id加入mdl_user AS u ON p.userid = u.id在哪里 c.enablecompletion = 1 AND p.course IN(1,2,3,4,5,6,7,8,9,10,11,12))
谢谢。
答案 0 :(得分:0)
换句话说,您正在寻找一个包含表A行的表,其中表A用户标识等于表B用户标识,而表A课程标识与表B课程标识不同,因此您的查询将是:
SELECT a.userid, a.courseid FROM a, b WHERE (a.userid = b.userid AND a.courseid != b.courseid) OR a.userid != b.userid
这应该有效
答案 1 :(得分:0)