MYSQL按列名排序,包含许多表左连接

时间:2014-06-15 05:02:54

标签: php mysql sql

我正在进行4个表合并,最后我希望它按" ORDER BY"进行排序。根据一些变量。现在,这总是返回相同的条目顺序。

类似的东西:

if(isset($_GET['filter'])){
    $filter = $_GET['filter'];
#Example $filter = 'date' or team or game_num

    $q = $db->prepare("SELECT g.game_num, s.date,  t.team
                              FROM schedule n
                              LEFT JOIN g_lkp g
                              ON n.game_num = g.game_num
                              LEFT JOIN dates s
                              ON n.date = s.date
                              LEFT JOIN teams t
                              ON n.home_team_nbr = t.team                                 
                              ORDER BY '$filter' ");
$q->execute();
$qR = $q->fetchAll(PDO::FETCH_ASSOC);
if ($q->rowCount() > 0) {   
    foreach ($qR as $row) {
        echo '

1 个答案:

答案 0 :(得分:1)

//First check value of $filter
$q = $db->prepare("SELECT g.game_num game_num, s.date date,  t.team team
                          FROM schedule n
                          LEFT JOIN g_lkp g
                          ON n.game_num = g.game_num
                          LEFT JOIN dates s
                          ON n.date = s.date
                          LEFT JOIN teams t
                          ON n.home_team_nbr = t.team                                 
                          ORDER BY $filter ");