按问题使用UNION命令的SQL查询

时间:2016-02-19 17:07:54

标签: jquery mysql

我想进行一个Order By查询,但我不知道为什么它不工作这是我的查询我不知道在哪里下订单,我测试了每一个选择,不工作,测试把它放在最后一个查询没有变化,你可以帮助我吗? 我希望通过a.id订购

$data = null;
                         $sth = Model::connect()->prepare(" SELECT a.*, b.ciudad, d.catgoria, d.categorie, e.mark AS marka , c.fuel FROM table1 a, 
                             fuel c ,ciudad b,  table2 d  , coches e WHERE 
                             a.ciudad = b.id_vil AND a.mark = e.id AND a.fuel = c.id AND d.id = a.table3 AND a.status = 1  

                             UNION 
                              SELECT a.*, b.ciudad, d.catgoria, d.categorie, e.mark AS marka , c.fuel FROM table1 a, 
                             fuel c ,ciudad b,  table2 d  , liste_vehicules_marchandises e WHERE 
                             a.ciudad = b.id_vil AND a.mark = e.id AND a.fuel = c.id AND  d.id = a.table3 AND a.status = 1  
                             UNION 
                             SELECT a.*, b.ciudad, d.catgoria, d.categorie, e.mark AS marka , c.fuel FROM table1 a, 
                             fuel c ,ciudad b,  table2 d  ,  liste_vehicules_agricultures e WHERE 
                             a.ciudad = b.id_vil AND a.mark = e.id AND a.fuel = c.id AND d.id = a.table3 AND a.status = 1  
                             UNION 
                              SELECT a.*, b.ciudad, d.catgoria, d.categorie, e.mark AS marka , c.fuel FROM table1 a, 
                             fuel c ,ciudad b,  table2 d  , bc_vehicule e WHERE 
                             a.ciudad = b.id_vil AND a.mark = e.id AND a.fuel = c.id AND d.id = a.table3 AND a.status = 1  


                             ");
                                                                    $sth->execute();
                                                                    $data = $sth->fetchAll();
                       return $data;

1 个答案:

答案 0 :(得分:1)

你会添加:

order by id

作为查询的最后一行。

此外,我建议您使用UNION ALL代替UNION,除非您特别想要产生删除重复项的开销。