Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 组合3条select语句并按时间戳排序_Php_Mysql_Sql_Sorting - Fatal编程技术网

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

Php 组合3条select语句并按时间戳排序,php,mysql,sql,sorting,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)

我有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);

要组合所有数据并按日期降序对它们进行排序,可以使用子查询和
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 |
+------+---------------------+
购买的用户

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
假设products表包含字段id、productname和productnumber。 已购买的had id、productid、purchasedate。 交货有id、purchaseid、交货日期

您可以通过执行以下操作组合每个查询中的所有字段,然后按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

“按时间戳desc从产品订单中选择*”;“内有分号?@M.chaudhry是有分号inside@M.chaudhry:
分号作为stmt终止符在引号内是合法的。@Ravinder我如何使上述要求生效?有史以来最好的解释。感谢我将在这里发布任何有关上述问题的新查询。我现在接受答案。再次感谢。