1

'Do' 方法用于准备好的语句,它返回(通过 fetchall()),我正在使用两个循环的语句(foreach()),有没有更好的代码(Sql),我可以在一个语句中获取两个表或查询?如果没有,那么我该怎么做才能使这段代码看起来更好。(PHP)

// 'id' is index in 'userinfo' table and 'id' in 'election' table is a child index(foreign key)
// 'ElectionId' is also the index, but it is not connected with othertable (only 'id' is linked)   

 <?php
    $call = new world();
    $get = $call->Do("SELECT Votes, Views, id FROM election WHERE ElectionId = ?", array("electionIdUknwn"));
        foreach($get as $show)
            {
                <div>"Votes is :".$show[0]</div>
                $add = $call->Do("SELECT username FROM userinfo WHERE id = ?", array($show[2]) );
                foreach($add as $display)
                {
                    <div>"Username is :".$display[0]</div>
                }
                <div>"Views are :".$show[1]</div>
            }         
  ?>
4

2 回答 2

2
SELECT e.Votes, e.Views, e.id, u.username
FROM   election e JOIN userinfo u USING (id)
WHERE  e.ElectionId = ?
于 2013-07-23T09:01:08.813 回答
2
SELECT e.Votes, e.Views, e.id, u.username
FROM election e INNER JOIN userinfo u ON u.id = e.id
WHERE e.ElectionId = ?
于 2013-07-23T09:03:20.647 回答