显示在底部的查询中的子查询结果

时间:2012-02-19 17:29:27

标签: php mysql

为标题道歉,我想要运行这样的查询:

$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的任务)显示在第一个查询结果下面(几乎就像是第一个查询的结果一样)。

原因是我有很多额外的功能可以改变你看到的任务数量,所以例如,如果用户选择查看一个结果然后点击下一步它当前经历了每个任务,我只想简单地追加第二个查询在第一个查询结束时生成,而不是有两组结果。

我可以将查询组合在一起,例如:

$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");

但是,使用duedate of 0000-00-00的任务将显示在顶部。

不确定最佳解决方案

干杯

5 个答案:

答案 0 :(得分: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"];
}    

我很久以前遇到类似的问题,所以我知道疼痛,但它应该有用......

答案 1 :(得分: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++;

    }

如果您只想要两个特定的行而不是在循环内部显示重新显示,这可能会显示多行,您可以替换标记为显示结果的部分,其中的代码部分将具有特定值的结果分配给数组或字符串变量然后在while循环之外显示它们。

希望能给你一个不错的指导方向。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

对来自2个查询的合并数据使用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") 

用于管理空字段的语句。

答案 4 :(得分:-1)

可能有两种方式:

查询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");

希望它有所帮助!