如何从3个表中的部分列数据中排序mysql查询输出


How to order mysql query output from partial column data in 3 tables

本质上,我试图将所有3个表中的驱动程序数据连接到一个输出查询中,该查询通过mysql使用上述列中的数据用regex组织。

我试图解决的问题与输出数据的排序有关。例如:我有3个表,这3个表中的每个表都有一个名为driver的列,该列具有相同的数据。不同之处在于驱动程序列中的"驱动程序/索引"。

有了这些数据,我计划根据司机的喜好重新排列单子和客户信息
例如

表 nbsp nbsp nbsp nbsp nbsp|列 nbsp 数据
DeliverySlip nbsp [驱动程序];(kevin/1)
提货单 nbsp [驱动程序] (凯文/3)
NewCustInfo;[驱动程序] nbsp;(kevin/2)


输出应该是:

  1. DeliverySlip
  2. 新客户信息
  3. 拾取滑动

而不是:

  1. DeliverySlip
  2. 拾取滑动
  3. 新客户信息

我的代码:

$driverget1="Kevin/1"; // this is grabbed through an SQL SELECT statement
$drivername=explode($driverget1,"/");
$tablearray2 = "DeliverySlip|PickupSlip|NewCustInfo";
$table2 = explode("|", $tablearray2);
$Query = "SELECT * FROM `bwa1`.`$table2[0]`,`bwa1`.`$table2[1]`,`bwa1`.`$table2[2]` WHERE `Driver` REGEXP '{$drivername[0]}/' AND `Accomplished`='0' ORDER BY `Driver` REGEXP '/[[:digit:]]' ASC";

当然,我犯了一个模棱两可的错误。所以,我在考虑使用JOIN语句。然而,根据我的理解,这似乎只是连接列而不是表。我只想根据NAME/INDEX对驱动程序进行排序,而忽略我从3个不同的表中获取信息的事实。对这些数据进行排序的最佳方式是什么?

更新:

我开始和UNION一起玩,这就是我想要的。下面的例子:

SELECT * FROM 
(
  SELECT NewCustInfo.Driver,NewCustInfo.id,NewCustInfo.Accomplished 
        FROM NewCustInfo WHERE NewCustInfo.Driver 
        REGEXP 'Test123/' AND NewCustInfo.Accomplished='0' 
        ORDER BY NewCustInfo.Driver REGEXP '/[[:digit:]]' ASC
) DUMMY_ALIAS1
UNION ALL
SELECT * FROM 
( 
  SELECT PickupSlip.Driver,PickupSlip.id,PickupSlip.Accomplished 
        FROM PickupSlip WHERE PickupSlip.Driver 
        REGEXP 'Test123/' AND PickupSlip.Accomplished='0' 
        ORDER BY PickupSlip.Driver REGEXP '/[[:digit:]]' ASC
) DUMMY_ALIAS2
UNION ALL
SELECT * FROM 
( 
  SELECT DeliverySlip.Driver,DeliverySlip.id,DeliverySlip.Accomplished 
        FROM DeliverySlip WHERE DeliverySlip.Driver 
        REGEXP 'Test123/' AND DeliverySlip.Accomplished='0' 
        ORDER BY DeliverySlip.Driver REGEXP '/[[:digit:]]' ASC
) DUMMY_ALIAS3

我仍然需要弄清楚,在完全联合后,每个项目最初来自哪个表。我唯一能想到的就是将表名插入Driver列,并用分隔符分隔,这样它就可以是REGEXP'd。

在select中添加表名如下:

SELECT * FROM  
(  
    SELECT NewCustInfo.Driver,NewCustInfo.id,NewCustInfo.Accomplished,'NewCustInfo' as tbname
    FROM NewCustInfo WHERE NewCustInfo.Driver 
    REGEXP 'Test123/' AND NewCustInfo.Accomplished='0' 
    ORDER BY NewCustInfo.Driver REGEXP '/[[:digit:]]' ASC
) DUMMY_ALIAS1
UNION ALL
SELECT * FROM 
( 
    SELECT PickupSlip.Driver,PickupSlip.id,PickupSlip.Accomplished,'PickupSlip' as tbname
    FROM PickupSlip WHERE PickupSlip.Driver 
    REGEXP 'Test123/' AND PickupSlip.Accomplished='0' 
    ORDER BY PickupSlip.Driver REGEXP '/[[:digit:]]' ASC
) DUMMY_ALIAS2
UNION ALL
SELECT * FROM 
( 
    SELECT DeliverySlip.Driver,DeliverySlip.id,DeliverySlip.Accomplished,'DeliveryShip' as tbname
    FROM DeliverySlip WHERE DeliverySlip.Driver 
    REGEXP 'Test123/' AND DeliverySlip.Accomplished='0' 
    ORDER BY DeliverySlip.Driver REGEXP '/[[:digit:]]' ASC
) DUMMY_ALIAS3
SELECT * FROM (SELECT PickupSlip.Driver, 
SUBSTRING_INDEX(PickupSlip.Driver, '/', -1) AS orderindex, 
SUBSTRING_INDEX(PickupSlip.Driver, '/', 1) AS thedriver,
WHERE PickupSlip.Driver 
REGEXP '{$driverget1}/' 
AND PickupSlip.Accomplished='0') DUMMY_ALIAS1
ORDER BY (orderindex+0) ASC

这就是我接受的代码。