Skip to content

Commit

Permalink
MDL-21852 Bye bye RIGHT JOINs
Browse files Browse the repository at this point in the history
RIGHT JOIN replaced with LEFT JOIN statement, SQL syntax and data
processing cleanup.
  • Loading branch information
mudrd8mz committed Jul 12, 2010
1 parent 000ebd5 commit 64d05f1
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions tag/manage.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,23 @@
$where = '';
}

$query = 'SELECT tg.id, tg.name, tg.rawname, tg.tagtype, COUNT(ti.id) AS count, u.id AS owner, tg.flag, tg.timemodified, u.firstname, u.lastname
FROM {tag_instance} ti RIGHT JOIN {tag} tg ON tg.id = ti.tagid LEFT JOIN {user} u ON tg.userid = u.id
'.$where.'
GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, u.id, tg.flag, tg.timemodified, u.firstname, u.lastname
'.$sort;
$totalcount = $DB->count_records_sql('SELECT COUNT(DISTINCT(tg.id))
FROM {tag} tg LEFT JOIN {user} u ON u.id = tg.userid '. $where, $params);
$query = "
SELECT tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
u.id AS owner, u.firstname, u.lastname,
COUNT(ti.id) AS count
FROM {tag} tg
LEFT JOIN {tag_instance} ti ON ti.tagid = tg.id
LEFT JOIN {user} u ON u.id = tg.userid
$where
GROUP BY tg.id, tg.name, tg.rawname, tg.tagtype, tg.flag, tg.timemodified,
u.id, u.firstname, u.lastname
$sort";

$totalcount = $DB->count_records_sql("
SELECT COUNT(DISTINCT(tg.id))
FROM {tag} tg
LEFT JOIN {user} u ON u.id = tg.userid
$where", $params);

$table->initialbars(true); // always initial bars
$table->pagesize($perpage, $totalcount);
Expand All @@ -244,11 +254,8 @@
//retrieve tags from DB
if ($tagrecords = $DB->get_records_sql($query, $params, $table->get_page_start(), $table->get_page_size())) {

$taglist = array_values($tagrecords);

//print_tag_cloud(array_values($DB->get_records_sql($query)), false);
//populate table with data
foreach ($taglist as $tag ){
foreach ($tagrecords as $tag) {
$id = $tag->id;
$name = '<a href="'.$CFG->wwwroot.'/tag/index.php?id='.$tag->id.'">'. tag_display_name($tag) .'</a>';
$owner = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$tag->owner.'">' . fullname($tag) . '</a>';
Expand Down

0 comments on commit 64d05f1

Please sign in to comment.