1

这是我目前遇到的问题。

我有 7 个行程数据表。我目前正在做的是从第三个表中随机选择一个 Card_ID,然后使用该 Card_ID 搜索所有 7 个表并选择由该特定 Card_ID 生成的所有记录。

问题是生成了许多结果集,这会带来问题,因为我将不得不手动将它们一一导出。有没有办法通过将大约 2000 个 WHILE 循环记录组合到一个结果集中来“自动化”这个过程?

提前致谢。

BEGIN
DECLARE counter INT;
DECLARE random INT;
DECLARE cardid VARCHAR(20);
SET counter = 1;

WHILE counter < 801 DO

    SET random = FLOOR(1 + (RAND() * 5451696));
    SET cardid = (SELECT CARD_ID FROM trips13042011 WHERE TripID = random);

    SELECT * FROM trips11042011 WHERE CARD_ID=cardid 
    UNION ALL
    SELECT * FROM trips12042011 WHERE CARD_ID=cardid
    UNION ALL
    SELECT * FROM trips13042011 WHERE CARD_ID=cardid
    UNION ALL
    SELECT * FROM trips14042011 WHERE CARD_ID=cardid    
    UNION ALL
    SELECT * FROM trips15042011 WHERE CARD_ID=cardid
    UNION ALL
    SELECT * FROM trips16042011 WHERE CARD_ID=cardid
    UNION ALL
    SELECT * FROM trips17042011 WHERE CARD_ID=cardid

    ORDER BY Ride_Start_Date, Ride_Start_Time ASC;

    SET counter = counter + 1;

end WHILE;
END
4

1 回答 1

2

我不熟悉您使用的语言(mySQL 脚本代码??),但据我了解,您可能会尝试这样的事情:

-做你的while循环来创建一个801(或任何你需要的)随机元素的数组。- 将数组添加到 where 子句中 -> WHERE CARD_ID IN (308, 3746, 72827, 1, etc...) 直到数组的第 801 个元素

DECLARE rdmArray ARRAY;

WHILE counter < 801 DO
     SET random = FLOOR(1 + (RAND() * 5451696));
     SET cardid = (SELECT CARD_ID FROM trips13042011 WHERE TripID = random);
     rdmArray.push_back(cardid );
     SET counter = counter + 1;
end WHILE;

SELECT * FROM trips11042011 WHERE CARD_ID IN (//element of rdmArray here) 
UNION ALL
SELECT * FROM trips12042011 WHERE CARD_ID IN (//element of rdmArray here)
UNION ALL
SELECT * FROM trips13042011 WHERE CARD_ID IN (//element of rdmArray here)
UNION ALL
SELECT * FROM trips14042011 WHERE CARD_ID IN (//element of rdmArray here)   
UNION ALL
SELECT * FROM trips15042011 WHERE CARD_ID IN (//element of rdmArray here)
UNION ALL
SELECT * FROM trips16042011 WHERE CARD_ID IN (//element of rdmArray here)
UNION ALL
SELECT * FROM trips17042011 WHERE CARD_ID IN (//element of rdmArray here)

ORDER BY Ride_Start_Date, Ride_Start_Time ASC;

...

根据我对您的问题的理解,这就是我认为需要做的事情。

希望这有帮助!

安托万

于 2013-01-31T05:28:30.233 回答