3

I want to know how joins are applied for $table= set_sql(); method. I'm trying to render a table without using the conventional html_table(); method.

A basic application for a single database table "mdl_user":

  $table->set_sql('*', "{user}", '1');

But I intend to achieve more complex sql query using joins as shown below:

    **SELECT aa.firstname, aa.email, zz.fullname
    FROM mdl_table1 aa
    INNER JOIN mdl_table2 zz
    ON aa.id = zz.userid WHERE lastlogin => ? and lastlogin <= ? GROUP BY firtname;**

This link below might be helpful. I've tried to work around it but still not clear about it. https://docs.moodle.org/dev/lib/tablelib.php

kcay King
  • 77
  • 7

1 Answers1

2

Something like this

$fields = 'aa.firstname, aa.email, zz.fullname',
$from = '{table1} aa
         INNER JOIN {table2} zz ON aa.id = zz.userid';
$where = 'lastlogin => :lastlogin1 and lastlogin <= :lastlogin2';
$params = array('lastlogin1' => $lastlogin, 'lastlogin2' => $lastlogin);
$table->set_sql($fields, $from, $where, $params);
Russell England
  • 9,436
  • 1
  • 27
  • 41