如何显示所有表(相同结构)的查询并按数据排序

时间:2022-03-20 15:41:30

I want to know how I can select query from more than 1 table to show and order then all by the data and limit then to a number(if I put LIMIT 5, to show only the most recently add by data 5 query from all the tables not the last recently add 5 of one of the tables, cause I want make like a newsbar) I have 5 tables(categories) with the same structure (id, title, descriere, poza, data) and I want to show only the last 5 of them all.

我想知道如何从多个表中选择查询以显示和按顺序排列所有数据并将其限制为一个数字(如果我放置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);?> 

but the code show me the last 5 query for every one tables and order by each other then comes the secont tables.. and want to order without take in calculation the first table....

但代码显示每个表的最后5个查询和彼此的顺序然后是secont表...并且想要订购而不需要计算第一个表....

1 个解决方案

#1


0  

Forget the foreach loop.

忘记foreach循环。

"SELECT id, titlu, data, linknews, poza FROM istorie, lifestyle, sciente, travel, nature  WHERE approved='1' ORDER BY data DESC LIMIT 5"

#1


0  

Forget the foreach loop.

忘记foreach循环。

"SELECT id, titlu, data, linknews, poza FROM istorie, lifestyle, sciente, travel, nature  WHERE approved='1' ORDER BY data DESC LIMIT 5"