2011-11-11 54 views
0

在dbase中我有幾個表,名字爲aaa_9xxx, aaa_9yyy, aaa_9zzz。我想查找指定的所有數據DATE,並用TIME ASC進行顯示。按照ASC排序的幾個MySQL表的數據

首先,我必須找到與dBASE一個表:

$STH_1a = $DBH->query("SELECT table_name 
        FROM information_schema.tables 
       WHERE table_name 
        LIKE 'aaa\_9%' 
       "); 


foreach($STH_1a as $row) 
{ 
$table_name_s1[] = $row['table_name']; 
} 

其次,我必須找到一個數據機智的具體日期,並TIME ASC表現出來:

foreach($table_name_s1 as $table_name_1) 
{ 
$STH_1a2 = $DBH->query("SELECT * 
         FROM `$table_name_1` 
        WHERE 
          date = '2011-11-11' 
        ORDER BY time ASC 
        "); 

while ($row = $STH_1a2->fetch(PDO::FETCH_ASSOC)) { 

echo " ".$table_name_1."-".$row['time']."-".$row['ei_name']." <br>"; 
} 
} 

..但它顯示按表格名稱排序的數據,然後按TIME ASC。我必須將所有這些數據(來自所有表)按TIME ASC排序。


謝謝dev-null-dweller,Andrew Stubbs和Jaison Erick的幫助。 我測試埃裏克解決方案:

foreach($STH_1a as $row) { 

$stmts[] = sprintf('SELECT * 
        FROM %s 
       WHERE date="%s"', $row['table_name'], '2011-11-11'); 

        } 

$stmt = implode("\nUNION\n", $stmts); 
$stmt .= "\nORDER BY time ASC"; 

$STH_1a2 = $DBH->query($stmt); 

while ($row_1a2 = $STH_1a2->fetch(PDO::FETCH_ASSOC)) { 

echo " ".$row['table_name']."-".$row_1a2['time']."-".$row_1a2['ei_name']." <br>"; 
} 

它的工作,但我有問題「table_name」 - 它總是LAST表名。

// -------------------------------------------- --------------------------

...和所有修補程序的結束解決方案,感謝所有的幫助,:))

foreach($STH_1a as $row) { 

$stmts[] = sprintf("SELECT *, '%s' AS table_name 
        FROM %s 
       WHERE date='%s'", $row['table_name'], $row['table_name'], '2011-11- 11'); 

} 


$stmt = implode("\nUNION\n", $stmts); 
$stmt .= "\nORDER BY time ASC"; 

$STH_1a2 = $DBH->query($stmt); 

while ($row_1a2 = $STH_1a2->fetch(PDO::FETCH_ASSOC)) { 

echo " ".$row_1a2['table_name']."-".$row_1a2['time']."-".$row_1a2['ei_name']." <br>"; 
} 

回答

0

你需要使用UNION SQL指令:

<?php 

$STH_1a = $DBH->query("SELECT table_name 
         FROM information_schema.tables 
         WHERE table_name 
         LIKE 'aaa\_9%'"); 

$stmts = array(); 
foreach($STH_1a as $row) 
{ 
    $stmts[] = sprintf('SELECT *, %s AS `table_name` FROM %s WHERE date="%s"', $row['table_name'], $row['table_name'], '2011-01-01'); 
} 

$stmt = implode("\nUNION\n", $stmts); 

$stmt .= "\nORDER BY time ASC"; 

$STH_1a2 = $DBH->query($stmt); 

FIX:包括表名作爲返回值。

+0

thnks,埃裏克,它的工作,但我有問題'table_name' - 信息上面。 – Andrew

+0

上面的修復將返回你想要的。 –

+0

Thnks,我的問題與您的修復。 Err顯示//致命錯誤:調用成員函數對非對象的fetch()//代碼while while($ row_1a2 = $ STH_1a2-> fetch(PDO :: FETCH_ASSOC)){ ' – Andrew

1

除了打印,你從數據庫獲取它的路線,聚集在一個陣列中的所有數據taht你將能夠與usort和您自己的回調函數來進行排序。

其他選項是讓直接從MySQL它有序​​,使用UNION選擇這樣的:

$SQL = " 
    (SELECT '$table_name_1' AS tbl_name, time, ei_name FROM `$table_name_1` WHERE date = '2011-11-11') 
    UNION 
    (SELECT '$table_name_2' AS tbl_name, time, ei_name FROM `$table_name_2` WHERE date = '2011-11-11') 
    UNION 
    (SELECT '$table_name_3' AS tbl_name, time, ei_name FROM `$table_name_3` WHERE date = '2011-11-11') 
    ORDER BY time ASC 
"; 
+0

謝謝,但我不知道表名 - 它必須在sql過程中生成。 – Andrew

+0

這就是爲什麼我將''table_name_3'AS tbl_name'添加到select中,這要歸功於您將在每個返回的行中都有表名稱。 –

0

解決這個問題的正確方法是聯合您的選擇數據,如其他答案中所述。

速戰速決將是你的第二個代碼塊更改爲類似:

$Sorted = array(); 

foreach($table_name_s1 as $table_name_1) 
{ 
    $STH_1a2 = $DBH->query("SELECT * 
     FROM `$table_name_1` 
     WHERE date = '2011-11-11' 
     ORDER BY time ASC 
    "); 

    while ($row = $STH_1a2->fetch(PDO::FETCH_ASSOC)) { 
     $Sorted[$row['time']] = " ".$table_name_1."-".$row['time']."-".$row['ei_name']." <br>"; 
    } 
} 

ksort($Sorted); 

foreach($Sorted as $Entry) { 
    echo $Entry; 
} 

注:這將「失敗」的地方有一個日期多個條目的情況。