2013-02-17 45 views
0

我有兩個數組從基本章節id中取出。其中一個數組處於章​​節介於1和6之間的情況。同樣,另一個數組處於同一表中章節介於7到12之間的情況。如何獲得單個數組中的數組而不會丟失mysql和php中的值?

在我的第一個查詢是

$sql1 = 'SELECT s.section_number, s.title,n.description,s.global_order_id, c.commentary FROM section as s 
       INNER JOIN note as n 
       INNER JOIN commentary as c ON s.global_order_id=n.global_order_id and c.global_order_id=n.global_order_id 
       where n.user_id='.Yii::app()->user->id.' and s.chapter_id Between 1 and 6'; 

和第二查詢

$sql2 = 'SELECT s.section_number, s.title,n.description,s.global_order_id, c.commentary FROM section as s 
       INNER JOIN note as n 
       INNER JOIN commentary as c ON s.global_order_id=n.global_order_id and c.global_order_id=n.global_order_id 
       where n.user_id=' . Yii::app()->user->id . ' and s.chapter_id Between 7 and 12'; 

所有相同按章什麼不同的是標題。

當我做數組合並比只顯示一個標題,但我想使雙數組單個顯示兩個標題。我該怎麼做?

在此先感謝...

回答

1

你可以在MySQL直接做到這一點,通過UNION(隱含不同)或UNION ALL兩個查詢,像:

SELECT 
    s.section_number, 
    s.title, 
    n.description, 
    s.global_order_id, 
    c.commentary 
FROM section as s 
INNER JOIN note as n 
INNER JOIN commentary as c ON s.global_order_id = n.global_order_id 
          and c.global_order_id = n.global_order_id 
where n.user_id = '.Yii::app()->user->id.' 
    and s.chapter_id Between 1 and 6 
UNION ALL 
SELECT 
    s.section_number, 
    s.title, 
    n.description, 
    s.global_order_id, 
    c.commentary 
FROM section as s 
INNER JOIN note as n 
INNER JOIN commentary as c ON s.global_order_id = n.global_order_id 
          and c.global_order_id = n.global_order_id 
where n.user_id=' . Yii::app()->user->id . ' 
    and s.chapter_id Between 7 and 12; 

然後執行此查詢只和你將把這兩個結果集合成一個來自這個查詢的數組。

+1

+1,你只需秒多打我 – Venu 2013-02-17 06:57:00

相關問題