2016-02-19 69 views
0

我想做一個命令查詢,但我不知道爲什麼它不工作這是我的查詢我不知道在哪裏把訂單,我測試與每一個選擇,不工作,經過測試把它放在最後一個查詢沒有變化,你能幫我嗎? 我想a.idSQL查詢與UNION命令的問題

$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; 
+0

什麼不工作,什麼是需要的行爲和實際發生的事情。樣本數據? –

回答

1

有訂單你可以這樣:

order by id 

,查詢中的最後一行。

此外,我會建議您使用UNION ALL而不是UNION,除非您特別要承擔刪除重複項的開銷。

+0

謝謝你的工作.. –