drupal查询使用INNER JOIN从{realname}添加变量?

时间:2018-04-10 09:39:00

标签: sql drupal drupal-7

我想将另一个表中的另一个变量添加到下面的查询中,为用户提供 {realname} 的真实姓名。

function opigno_statistics_app_query_course_students_results($course_nid) {
  $cache_key = __FUNCTION__ . ':' . $course_nid;
  $cached_object = cache_get($cache_key);
  if($cached_object) {
    $students_results = $cached_object->data;
  } else {
    $students_results = array();
    $result = db_query("
      SELECT uc.username as student_name, j1.page_views as number_of_interactions, uc.score, uc.status, uc.uid
      FROM {opigno_statistics_user_course} uc, {realname} AS rn
      INNER JOIN (
        SELECT SUM(page_views) as page_views, uid
        FROM {opigno_statistics_user_group} ug
        WHERE group_nid = :course_nid
        GROUP BY uid
      ) j1 ON uc.uid = j1.uid
      WHERE course_nid = :course_nid
    ", array(
      ':course_nid' => $course_nid
    ));

由于它使用INNER JOIN,我是否需要添加另一个或者下面的工作?

$students_results = array();
    $result = db_query("
      SELECT uc.username as student_name, j1.page_views as number_of_interactions, uc.score, uc.status, uc.uid, 
rn.realname as student_realname // ADDED
      FROM {opigno_statistics_user_course} uc, 
           {realname} AS rn  // ADDED
      INNER JOIN (
        SELECT SUM(page_views) as page_views, uid
        FROM {opigno_statistics_user_group} ug
        WHERE group_nid = :course_nid
        GROUP BY uid
      ) j1 ON uc.uid = j1.uid

      rn ON uc.uid = rn.uid // ADDED

      WHERE course_nid = :course_nid
    ", array(
      ':course_nid' => $course_nid
    ));

1 个答案:

答案 0 :(得分:0)

最后管理解决它。可以有多个INNER JOINS,只需要定义它们:)

$result = db_query("
      SELECT uc.username as student_name, j1.page_views as number_of_interactions, uc.score, uc.status, uc.uid, 
j2.realname as student_realname
      FROM {opigno_statistics_user_course} uc
      INNER JOIN (
        SELECT SUM(page_views) as page_views, uid
        FROM {opigno_statistics_user_group} ug
        WHERE group_nid = :course_nid
        GROUP BY uid
      ) j1 ON uc.uid = j1.uid
      INNER JOIN (
        SELECT(realname) as realname, uid
        FROM {realname} realn
      ) j2 ON uc.uid = j2.uid
      WHERE course_nid = :course_nid
    ", array(':course_nid' => $course_nid));