![](/img/trans.png)
[英]How to SELECT all the tables mySql of a db and show query order by data, tables have the same structure
[英]How to show query from all the tables(same structure) and order all by data
我想知道如何從多個表中選擇查詢以顯示和按順序排列所有數據並將其限制為一個數字(如果我放置LIMIT 5,僅顯示最近由數據5查詢添加來自所有表不是最近一次添加其中一個表的5個,因為我想像新聞欄一樣制作)我有5個表(類別)具有相同的結構(id,title,descriere,poza,data),我只想顯示最后5個全部。
//create connection to mysql
$db_conx_index = mysqli_connect($servername, $username, $password, $dbname);
$db_conx_index->set_charset('utf8');// pentru diacritice
if(!$db_conx_index){
die("Connection failed: ". mysqli_connect_error());
}
$catArray = array("istorie", "lifestyle", "sciente", "travel", "nature");
$slider = '';
foreach($catArray as $k=>$v)
{
$sql = "SELECT id, titlu, data, linknews, poza FROM ".$v." WHERE approved='1' ORDER BY data DESC LIMIT 5";
$query = mysqli_query($db_conx_index, $sql);
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["id"];
$titlu = $row["titlu"];
$linknews = $row["linknews"];
$poza = $row["poza"];
$slider .= '<a href="/'.$linknews.'"><img src="/images/'.$poza.'-600x.jpg" alt="'.$titlu.'"></a>';
}
}
?>
<?php echo $slider;?>
<?php mysqli_close($db_conx_index);?>
但代碼顯示每個表的最后5個查詢和彼此的順序然后是secont表...並且想要訂購而不需要計算第一個表....
忘記foreach
循環。
"SELECT id, titlu, data, linknews, poza FROM istorie, lifestyle, sciente, travel, nature WHERE approved='1' ORDER BY data DESC LIMIT 5"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.