这是我目前遇到的问题。
我有 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