组合3个select语句并按时间戳排序

时间:2014-03-25 23:17:05

标签: php mysql sql sorting

我有3个mysql语句,每个语句给出不同的结果集,我想表示从按时间戳desc排序的查询中收集的所有结果。

例如:如果购买的订单在产品之前有时间戳,则购买订单中的商品应该在产品之前 我该怎么做并发送了3个结果集的组合排序结果。我非常感谢任何帮助。谢谢你。

$query=mysql_query("select * from products order by timestamp desc ;");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
    $rows = array();
    while($r = mysql_fetch_assoc($query)) {
        $rows[] = $r;
    }
}
echo json_encode($rows);

$query2=mysql_query("select * from user_purchased order by timestamp desc ;");
$numrows2 = mysql_num_rows($query2);
if ($numrows2!=0)
{
    $rows2 = array();
    while($r2 = mysql_fetch_assoc($query2)) {
        $rows2[] = $2r;
    }
}
echo json_encode($rows2);

$query3=mysql_query("select * from delivery order by timestamp desc ;");
$numrows3 = mysql_num_rows($query3);
if ($numrows3!=0)
{
    $rows3 = array();
    while($r3 = mysql_fetch_assoc($query3)) {
        $rows3[] = $r3;
    }
}
echo json_encode($rows3);

1 个答案:

答案 0 :(得分:1)

要合并所有数据并按日期降序对它们进行排序,您将使用带有UNION ALL的子查询并合并来自所有3个表的未排序结果...然后按时间戳降序排序记录。以下是一个例子。但是,如果它们都具有不同的字段名称且字段名称的数量不同,则可以创建虚拟字段名称以创建一致的信息。它在下面的编辑中描述。

为简单起见,我们假设所有表都具有以下结构

+-------+-----------+------+-----+-------------------+-------+
| Field | Type      | Null | Key | Default           | Extra |
+-------+-----------+------+-----+-------------------+-------+
| id    | int(11)   | YES  |     | NULL              |       |
| ts    | timestamp | NO   |     | CURRENT_TIMESTAMP |       |
+-------+-----------+------+-----+-------------------+-------+

让我们看看每张表中的虚拟数据。

<强>产品

mysql> select * from products;
+------+---------------------+
| id   | ts                  |
+------+---------------------+
|    1 | 2014-03-26 04:39:06 |
|    4 | 2014-03-26 04:40:05 |
+------+---------------------+

<强> User_Purchased

mysql> select * from user_purchased;
+------+---------------------+
| id   | ts                  |
+------+---------------------+
|    2 | 2014-03-26 04:39:19 |
|    5 | 2014-03-26 04:40:00 |
+------+---------------------+

投放

mysql> select * from delivery;
+------+---------------------+
| id   | ts                  |
+------+---------------------+
|    3 | 2014-03-26 04:39:28 |
|    6 | 2014-03-26 04:39:38 |
+------+---------------------+

<强> PHP

<?php
$db = mysqli_connect('host','usr','pwd');
mysqli_select_db($db,'yourdb');
$data = mysqli_query($db, "
    select * from
    (
        select 'products' as tbl, id, ts from products
        union all
        select 'purchased' as tbl, id, ts from user_purchased
        union all
        select 'delivered' as tbl, id, ts from delivery
    ) a
    order by ts desc
    "
);

echo "tablename\tid\ttime\n";
foreach ($data as $row) {
    echo "{$row['tbl']}\t{$row['id']}\t{$row['ts']}\n";
}
?>

<强>输出

tablename       id      time
products        4       2014-03-26 04:40:05
purchased       5       2014-03-26 04:40:00
delivered       6       2014-03-26 04:39:38
delivered       3       2014-03-26 04:39:28
purchased       2       2014-03-26 04:39:19
products        1       2014-03-26 04:39:06

修改

我们说产品表有字段id,productname,productnumber。 购买了id,productid,purchaseate。 交货有id,purchaseid,deliverydate。

您可以通过执行类似的操作来组合每个查询中的所有字段,然后对mytimestamp进行排序:

select 
    'products' as tbl, pdt_id, productname as pdt_productname, productnumber as pdt_productnumber,
    '-' as pur_id, '-' as pur_productid, '-' as pur_purchasedate,
    '-' as del_id, '-' as del_purchaseid, '-' as del_deliverydate,
    ts as mytimestamp
from products
union all
select 
    'purchased' as tbl, '-' as pdt_id, '-' as pdt_productname, '-' as pdt_productnumber, 
    id as pur_id, productid as pur_productid, purchasedate as pur_purchasedate,
    '-' as del_id, '-' as del_purchaseid, '-' as del_deliverydate,
    ts as mytimestamp
from purchased
union all
select 
    'delivered' as tbl, '-' as pdt_id, '-' as pdt_productname, '-' as pdt_productnumber, 
    '-' as pur_id, '-' as pur_productid, '-' as pur_purchasedate,
    id as del_id, purchaseid as del_purchaseid, deliverydate as del_deliverydate,
    ts as mytimestamp
from delivered