以下代码段有效,但我觉得可以通过缩短并因此提高效率来进一步改进,但我很难想到可以修改的内容。
以下是代码。
function get_student_stakeholder_refs($debate_id) {
$sql1 = "SELECT
b.id AS debates_id,
c.id AS stakeholder_id,
a.student_id AS student_id
FROM
debate_groups a
inner join debates b ON a.debate_id = b.id
inner join debate_stakeholders c ON a.stakeholder_id = c.id
WHERE
b.starter_pack_id = c.starter_pack_id AND
a.debate_id = b.id AND
b.id = ? AND
a.student_id = ?";
$rslt = $this->db->query($sql1, array($debate_id, $this->get_user_id()))->row();
if(count($rslt)!= 0){
$sql2 = "SELECT * FROM debate_stakeholder_profiles WHERE stakeholder_id = ?";
return $this->db->query($sql2, array($rslt->stakeholder_id))->result_array();
}
}
有没有办法将以下SQL语句合并为一个 SQL语句,以便更容易阅读?
答案 0 :(得分:1)
对我来说,似乎你不需要第二个SQL语句,因为你可以在第一个语句中获取连接中的整行。只需替换它:
c.id AS stakeholder_id,
有了这个:
c.*