More efficient and compatible SQL query (#1737)

Don't do a nested select, it adds unnecessary overload to the db. Instead, use a left join, quicker and more compatible among db types.
This commit is contained in:
Luis Daniel Lucio Quiroz 2016-07-06 14:36:18 -04:00 committed by FusionPBX
parent 230257fff9
commit e6567d6d8d
1 changed files with 5 additions and 4 deletions

View File

@ -107,13 +107,14 @@ include "root.php";
//get moh records, build array
$sql = "select ";
$sql .= "(select domain_name from v_domains as d where domain_uuid = m.domain_uuid) as domain_name, * ";
$sql .= "d.domain_name, m.* ";
$sql .= "from v_music_on_hold as m ";
$sql .= "where domain_uuid = '".$this->domain_uuid."' ";
$sql .= "left join v_domains as d ON d.domain_uuid = m.domain_uuid ";
$sql .= "where m.domain_uuid = '".$this->domain_uuid."' ";
if (permission_exists('music_on_hold_domain')) {
$sql .= "or domain_uuid is null ";
$sql .= "or m.domain_uuid is null ";
}
$sql .= "order by domain_uuid desc, music_on_hold_rate asc, music_on_hold_name asc";
$sql .= "order by m.domain_uuid desc, music_on_hold_rate asc, music_on_hold_name asc";
$prep_statement = $this->db->prepare(check_sql($sql));
$prep_statement->execute();
return $prep_statement->fetchAll(PDO::FETCH_NAMED);