我有一个返回许多行的查询:
SELECT c.id AS courseID,
c.fullname AS courseName,
c.continuingeducationcredits AS cec,
cc.name AS catName,
IFNULL(ccp.name, '-No Parent Category-') AS catParent,
ue.userid AS userID,
CONCAT(u.firstname, ' ', u.lastname) AS name,
ud.data AS employeeid,
date_format(date_add('1970-01-01',interval round(c.startdate/60/60/24) DAY), '%m/%d/%Y') AS startdate,
u.id AS uid,
c.startdate AS cstartdate
FROM user_enrolments AS ue
INNER
JOIN USER AS u ON ue.userid = u.id
INNER JOIN user_info_data AS ud ON (ud.fieldid = 1
AND ud.userid = u.id)
INNER JOIN enrol AS e ON e.id = ue.enrolid
INNER JOIN course AS c ON c.id = e.courseid
INNER JOIN course_categories AS cc ON cc.id = c.category
LEFT
OUTER JOIN course_categories AS ccp ON ccp.id = cc.parent
WHERE 1 = 1
AND (cc.ID = 13
OR ccp.ID = 13)
ORDER BY catParent,
uid,
cstartdate DESC
这工作正常,并返回多行。但是,如果我尝试将其用作子查询:
SELECT *
FROM
(SELECT c.id AS courseID,
c.fullname AS courseName,
c.continuingeducationcredits AS cec,
cc.name AS catName,
IFNULL(ccp.name, '-NO Parent Category-') AS catParent, ue.userid AS userID, CONCAT(u.firstname, ' ', u.lastname) AS name,
ud.data AS employeeid,
date_format(date_add('1970-01-01',interval round(c.startdate/60/60/24) DAY), '%m/%d/%Y') AS startdate,
u.id AS uid,
c.startdate AS cstartdate
FROM user_enrolments AS ue
INNER JOIN USER AS u ON ue.userid = u.id
INNER JOIN user_info_data AS ud ON (ud.fieldid = 1
AND ud.userid = u.id)
INNER JOIN enrol AS e ON e.id = ue.enrolid
INNER JOIN course AS c ON c.id = e.courseid
INNER
JOIN course_categories AS cc ON cc.id = c.category
LEFT OUTER JOIN course_categories AS ccp ON ccp.id = cc.parent
WHERE 1 = 1
AND (cc.ID = 13
OR ccp.ID = 13)) AS Pseudotable
WHERE courseID = 8946
ORDER BY catParent,
uid,
cstartdate DESC
它只会返回一行。
我究竟做错了什么?:(
第一个查询是第二个查询的内部查询。
第一个查询获取参加过某个类别中课程的所有用户。第二个旨在过滤该集合,并仅获取参加过单个课程的用户。