简体   繁体   English

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

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

Essentially i'm trying to concatenate the Driver data from all 3 tables into one output query organized with regex through mysql using data from said columns. 本质上,我正在尝试使用上述列中的数据,将所有3个表中的Driver数据连接到一个通过regex通过mysql组织的输出查询中。

The issue i'm trying to solve has to do with the ordering of my output data. 我要解决的问题与我的输出数据的排序有关。 for example: i have 3 tables, each of these 3 tables have a column called driver with the same data. 例如:我有3个表,这3个表中的每一个都有一个称为driver的列,具有相同的数据。 the difference being the "driver/index" in the driver column. 区别在于驱动程序列中的“驱动程序/索引”。

With this data I plan to rearrange slips and customer information to the drivers liking. 借助这些数据,我计划将单据和客户信息重新安排给喜欢的驾驶员。
eg 例如

Table |Column |Data 表|列|数据
DeliverySlip [Driver] (kevin/1) DeliverySlip [驱动程序](kevin / 1)
PickupSlip [Driver] (kevin/3) PickupSlip [驱动程序](kevin / 3)
NewCustInfo [Driver] (kevin/2) NewCustInfo [驱动程序](kevin / 2)


output should be: 输出应为:

  1. DeliverySlip 送货单
  2. NewCustInfo NewCustInfo
  3. PickupSlip PickupSlip

instead of: 代替:

  1. DeliverySlip 送货单
  2. PickupSlip PickupSlip
  3. NewCustInfo NewCustInfo

my code: 我的代码:

$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";

of course i get an ambiguous error. 当然我得到一个模棱两可的错误。 so, I was thinking about using the JOIN statements. 因此,我正在考虑使用JOIN语句。 however that only seems to join columns instead of tables from my understanding. 但是从我的理解来看,这似乎只是联接列而不是表。 I just want to sort the drivers based on the NAME/INDEX and ignore the fact that i'm grabbing information from 3 different tables. 我只想根据NAME / INDEX对驱动程序进行排序,而忽略了我从3个不同的表中获取信息的事实。 What is the best way to sort this data? 排序此数据的最佳方法是什么?

UPDATE: 更新:

I started playing around with UNION, this is what i was looking for. 我开始和UNION一起玩耍,这就是我想要的。 an example below: 下面的例子:

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

I still need to figure out from which table each item came from originally after the full union. 我仍然需要弄清楚每个项目最初是从哪张桌子来的。 Only thing I can think of is to insert the table name into the Driver column and seperate that with a delimiter so that it can be REGEXP'd. 我唯一能想到的就是将表名插入Driver列,并用定界符将其分隔,以便可以进行REGEXP处理。

how about adding table name in select like this: 像这样在选择中添加表名如何:

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

This was the code i settled for. 这是我确定的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM