2012-02-19 92 views
1

道歉標題,我想運行像這樣的查詢:子查詢結果withing一個查詢,顯示在底部

$query = mysql_query(" 
    select * from taskdetail 
    where userid = '" . $rec['id'] . "' 
    and comp_status = '0' 
    and duedate > '" . date("Y-m-d") . "' 
    order by duedate,importantlevel ASC 
    LIMIT " . $getnextrecord . " , $limit 
"); 

,然後運行一個像這樣

$query_noduedate = mysql_query(" 
    select * from taskdetail 
    where userid = '" . $rec['id'] . "' 
    and comp_status = '0' 
    and duedate = '0000-00-00' 
") or die(mysql_error()); 

我希望第二個查詢的結果(沒有duedate的任務)出現在第一個查詢結果下面(幾乎就好像它是第一個查詢的結果)。

的原因是被我有很多能改變你看到的任務數,額外functionailty如此例如,如果用戶不得不選擇看到一個結果,然後單擊下一步它目前經過每一個任務,我的simpy想種將第二個查詢結果附加到第一個查詢結果而不是有兩組結果。

我可以在查詢組合在一起,例如:

$query = mysql_query(" 
    select * from taskdetail 
    where userid='" . $rec['id'] . "' 
    and comp_status='0' 
    and (duedate > '" . date("Y-m-d") . "' or duedate = '0000-00-00') 
    order by duedate,importantlevel ASC 
    LIMIT " . $getnextrecord . " , $limit"); 

然而,隨着0000-00-00的交貨期的任務將出現在頂部。

不知道的最佳解決方案

乾杯

+0

請不要這樣做http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in-php – dynamic 2012-02-19 17:59:31

回答

-1

可能有2種方法:

查詢1 - 通過修改ORDER BY

$query = mysql_query("select * 
    from taskdetail 
    where userid='" . $rec['id'] . "' 
    and comp_status='0' 
    and (duedate > '" . date("Y-m-d") . "' or duedate = '0000-00-00') 
    order by (CASE 
     WHEN duedate > '0000-00-00' THEN duedate 
     ELSE '9999-99-99' 
     END), importantlevel ASC 
    LIMIT " . $getnextrecord . " , $limit"); 

查詢2 - 添加另一列 'CUSTOM_DATE' 的SELECT和使用它的ORDER BY

$query = mysql_query("select *, (CASE 
     WHEN duedate > '0000-00-00' THEN duedate 
     ELSE '9999-99-99' 
     END) AS `custom_date` 
    from taskdetail 
    where userid='" . $rec['id'] . "' 
    and comp_status='0' 
    and (duedate > '" . date("Y-m-d") . "' or duedate = '0000-00-00') 
    order by `custom_date`, importantlevel ASC 
    LIMIT " . $getnextrecord . " , $limit"); 

希望它能幫助!

+0

非常感謝你,查詢1對我來說是最容易實現的,工作一種享受,謝謝 – AJFMEDIA 2012-02-19 18:12:42

+0

偉大的工作:-) – Abhay 2012-02-19 18:18:22

+0

這是exctreme不夠高效! – Kamil 2012-02-19 18:38:25

1

有這就是我想出什麼:

$idnum = 0; //New count 
$tasklist = array(); //New task array 
while($task_with_due = mysql_fetch_array($query)){ //Add each task with due date to array 
    $tasklist[$idnum] = $task_with_due; 
    $idnum++; 
} 
while($task_with_nodue = mysql_fetch_array($query_noduedate)){ //Add each task with no due date to array 
    $tasklist[$idnum] = $task_with_nodue; 
    $idnum++; 
} 

foreach($task as $tasklist){ //Echo each task field 
    echo $task["name_of_the_field_in_db"]; 
}  

我不是很久以前有很多的問題,類似的東西,所以我知道疼痛但它應該工作...

0
ORDER BY duedate = '0000-00-00' -- 0 if false, 1 if true 
     duedate, 
     importantlevel ASC 
1

您可以嘗試while循環來處理數據行。

//Your first query 
    $query = mysql_query("select * from taskdetail where userid='" . $rec['id'] . "' and comp_status='0' and duedate > '" . date("Y-m-d") . "' order by duedate,importantlevel ASC LIMIT " . $getnextrecord . " , $limit"); 

    //Now perform what you want with each row of results 
    //Cycle through each row of results 
    while($row = mysql_fetch_array($query)){ 
     //assign the result sest to the $row array 
     extract($row); 

     //Here would go your code to display what you want from the first results which are now stored in the $row array with keys starting at 0 

     //Your sub query 
     $query = mysql_query("select * from taskdetail where userid='" . $rec['id'] . "' and comp_status='0' and (duedate > '" . date("Y-m-d") . "' or duedate = '0000-00-00') order by duedate,importantlevel ASC LIMIT " . $getnextrecord . " , $limit"); 

     //Here is where you would display what you want from the second query. If you have multiple results you could duplicate they while loop from above. 

$i++; 

    } 

如果你只想要兩個特定的行,而不是顯示一個循環可以顯示幾行,你可以取代被標記出來,以顯示與該分配結果與特定的值的代碼段的結果段內的reuslts數組或字符串變量,然後將它們顯示在while循環之外。

希望能給你一個體面的開始方向。從2合併數據

0

使用union查詢

select a,b,c,d from table1 order by a 
union 
select a,b,c,d from table2 order by a; 

或者

使用

ifnull(columnThatCanBeNull, "value that will replace nulls") 

語句來管理空字段。