如何从多个 table 中获取数据
How to fetch datas from multiple table
我有三个不同的表,我们将它们命名为
表1,
表 2,
表 3
但是在这些不同的表中,我有相同的列数和相同的名称。例如,
Table 1 列:id、标题、类别、子类别、图片
Table 2列:id, title, category, subcategory, image
Table 3列:id, title, category, subcategory, image
如何使用单个 foreach 获取所有这 3 个表?
function post_details(){
global $db;
$query = "SELECT * FROM posts WHERE status = 'Active' ORDER BY id DESC";
$run_query = mysqli_query($db, $query);
$post_details = mysqli_fetch_all($run_query, MYSQLI_ASSOC);
$getdetails = array();
foreach ($post_details as $post_detail) {
array_push($getdetails, $post_detail);
}
return $getdetails;
}
如果您的页面显示空白,请将其添加到 php 文件的顶部
//if error happend,this will display error on page
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
如果你想简单地循环行,你可以使用mysql union
function post_details(){
global $db;
$query = "SELECT * FROM posts1 WHERE status = 'Active' ORDER BY id DESC";
$query .= " UNION SELECT * FROM posts2 WHERE status = 'Active' ORDER BY id DESC";
$query .= " UNION SELECT * FROM posts3 WHERE status = 'Active' ORDER BY id DESC";
$run_query = mysqli_query($db, $query);
$post_details = mysqli_fetch_all($run_query, MYSQLI_ASSOC);
$getdetails = array();
foreach ($post_details as $post_detail) {
array_push($getdetails, $post_detail);
}
return $getdetails;
}
如果你想按所有人排序 table 你可以像这样使用临时 table,注意这一点,在大数据集中它可能会很慢,因为它排序临时 table.
$query = "SELECT * FROM
(SELECT * FROM posts1 WHERE status = 'Active'
UNION SELECT * FROM posts2 WHERE status = 'Active'
UNION SELECT * FROM posts3 WHERE status = 'Active')
AS t1 ORDER BY id DESC";
实际上,当我们想从多个table中获取具有相同列或相同table但条件不同的记录时,我们需要使用 UNION 将所有这些记录合并到相同的 SQL 查询,因此请使用此查询并将 table 名称替换为您的真实 table 名称。
$query = "SELECT * FROM ( ( SELECT * FROM table1 WHERE status = 'Active' ORDER BY id DESC )
UNION
( SELECT * FROM table2 WHERE status = 'Active' ORDER BY id DESC )
UNION
( SELECT * FROM table2 WHERE status = 'Active' ORDER BY id DESC ) ) as tempdata";
我有三个不同的表,我们将它们命名为
表1, 表 2, 表 3
但是在这些不同的表中,我有相同的列数和相同的名称。例如,
Table 1 列:id、标题、类别、子类别、图片
Table 2列:id, title, category, subcategory, image
Table 3列:id, title, category, subcategory, image
如何使用单个 foreach 获取所有这 3 个表?
function post_details(){
global $db;
$query = "SELECT * FROM posts WHERE status = 'Active' ORDER BY id DESC";
$run_query = mysqli_query($db, $query);
$post_details = mysqli_fetch_all($run_query, MYSQLI_ASSOC);
$getdetails = array();
foreach ($post_details as $post_detail) {
array_push($getdetails, $post_detail);
}
return $getdetails;
}
如果您的页面显示空白,请将其添加到 php 文件的顶部
//if error happend,this will display error on page
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
如果你想简单地循环行,你可以使用mysql union
function post_details(){
global $db;
$query = "SELECT * FROM posts1 WHERE status = 'Active' ORDER BY id DESC";
$query .= " UNION SELECT * FROM posts2 WHERE status = 'Active' ORDER BY id DESC";
$query .= " UNION SELECT * FROM posts3 WHERE status = 'Active' ORDER BY id DESC";
$run_query = mysqli_query($db, $query);
$post_details = mysqli_fetch_all($run_query, MYSQLI_ASSOC);
$getdetails = array();
foreach ($post_details as $post_detail) {
array_push($getdetails, $post_detail);
}
return $getdetails;
}
如果你想按所有人排序 table 你可以像这样使用临时 table,注意这一点,在大数据集中它可能会很慢,因为它排序临时 table.
$query = "SELECT * FROM
(SELECT * FROM posts1 WHERE status = 'Active'
UNION SELECT * FROM posts2 WHERE status = 'Active'
UNION SELECT * FROM posts3 WHERE status = 'Active')
AS t1 ORDER BY id DESC";
实际上,当我们想从多个table中获取具有相同列或相同table但条件不同的记录时,我们需要使用 UNION 将所有这些记录合并到相同的 SQL 查询,因此请使用此查询并将 table 名称替换为您的真实 table 名称。
$query = "SELECT * FROM ( ( SELECT * FROM table1 WHERE status = 'Active' ORDER BY id DESC )
UNION
( SELECT * FROM table2 WHERE status = 'Active' ORDER BY id DESC )
UNION
( SELECT * FROM table2 WHERE status = 'Active' ORDER BY id DESC ) ) as tempdata";