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.
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. 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)
PickupSlip [Driver] (kevin/3)
NewCustInfo [Driver] (kevin/2)
output should be:
instead of:
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. 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. What is the best way to sort this data?
UPDATE:
I started playing around with UNION, this is what i was looking for. 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.