我想知道如何从多个表中选择查询以显示和按顺序排列所有数据并将其限制为一个数字(如果我放置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表..并且想要在不计算第一个表的情况下订购....
答案 0 :(得分:0)
忘记foreach
循环。
"SELECT id, titlu, data, linknews, poza FROM istorie, lifestyle, sciente, travel, nature WHERE approved='1' ORDER BY data DESC LIMIT 5"