0

我有一个这样的选择语句:

    SELECT c.id AS courseid, u.id AS userid

    FROM

    mdl_user_enrolments ue
    join mdl_enrol e on e.id = ue.enrolid
    join mdl_user u on u.id = ue.userid
    join mdl_course c on c.id = e.courseid

我有另一条与当前表没有直接关系的数据。我一直在输出循环中调用这个函数,

function getLastCourseAccessByUser($courseid, $userid){
global $DB;
return
    $DB->get_record_sql('
    SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
    FROM mdl_log
    WHERE course = '.$courseid.' AND userid = '.$userid.'

    ORDER BY time DESC
    limit 1
');

}

但宁愿一次性获取所有数据,而不是回调数据库。

我正在尝试这个:

            SELECT c.id AS courseid, u.id as userid

            (
               SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
               FROM mdl_log
               WHERE course = c.id AND userid = u.id 
            )  AS lastaccessdate

        FROM

         mdl_user_enrolments ue
         join mdl_enrol e on e.id = ue.enrolid
         join mdl_user u on u.id = ue.userid
         join mdl_course c on c.id = e.courseid

或者,我可以像这样直接从 sql 调用函数吗:

        SELECT c.id AS courseid, u.id as userid,

             '.getLastCourseAccessByUser(c.id,u.id).' AS lastaccessdate

    FROM

        mdl_user_enrolments ue
        join mdl_enrol e on e.id = ue.enrolid
        join mdl_user u on u.id = ue.userid
        join mdl_course c on c.id = e.courseid

谢谢你。

4

2 回答 2

2
SELECT c.id AS courseid, u.id AS userid, FROM_UNIXTIME(t.time, "%m/%d/%Y") as ftime

    FROM

    mdl_user_enrolments ue
    join mdl_enrol e on e.id = ue.enrolid
    join mdl_user u on u.id = ue.userid
    join mdl_course c on c.id = e.courseid
    join mdl_log t on t.course = c.id and t.userid = u.id

您可以将 AND 和 OR 子句放在 mysql 的连接中,这样您就可以同时连接两个条件。尽管您可能希望将其设为外连接,以便在不满足条件时不会使整个查询失败

否则,您的子选择选项也应该起作用(这个)

 SELECT c.id AS courseid, u.id as userid

            (
               SELECT FROM_UNIXTIME(time, "%m/%d/%Y") as ftime
               FROM mdl_log
               WHERE course = c.id AND userid = u.id 
            )  AS lastaccessdate

        FROM

         mdl_user_enrolments ue
         join mdl_enrol e on e.id = ue.enrolid
         join mdl_user u on u.id = ue.userid
         join mdl_course c on c.id = e.courseid
于 2013-05-09T15:00:10.150 回答
1

您在 MySQL Server 端处理的数据越多,MySQL Server 和您的 PHP 应用程序之间产生的流量就越少,从而加快了应用程序的速度。我会说一次从服务器获取所有数据,而不是来回多次。

getLastCourseAccessByUser如果您需要在其他应用程序中重用该函数,您还可以在 MySQL 中将该函数创建为存储函数:

DELIMITER \\
DROP FUNCTION IF EXISTS getLastCourseAccessByUser\\
CREATE FUNCTION getLastCourseAccessByUser (in_course_id INT, in_user_id VARCHAR(50) 
RETURNS STRING
BEGIN
    SELECT FROM_UNIXTIME(time, "%m/%d/%Y") AS ftime
    FROM mdl_log
    WHERE course = in_course_id AND userid = in_user_id 
    ORDER BY time DESC
    LIMIT 1
END\\
DELIMITER ;

然后你可以在你的SELECT声明中使用它:

SELECT c.id AS courseid, 
       u.id as userid,
       getLastCourseAccessByUser(c.id,u.id) AS lastaccessdate
  FROM mdl_user_enrolments ue
  JOIN mdl_enrol e on e.id = ue.enrolid
  JOIN mdl_user u on u.id = ue.userid
  JOIN mdl_course c on c.id = e.courseid
于 2013-05-09T15:01:43.377 回答