+-----+--------+--------+
| id | svip | svupdatetime |
+-----+--------+--------+
| 1  | aaa  | 20111124  |
| 2  | aaa  | 20111123  |
| 3  | bbb  | 20111122  |
原有代码:
$favorites_query = DB::query("SELECT * FROM ".DB::table('favorites')."  WHERE ID IN
( select max(id) from ".DB::table('favorites')." GROUP BY svip )
ORDER BY svupdatetime DESC LIMIT $start_limit, $perpage");查询结果:
------------------
aaa
bbb由于程序禁止子查询,需要将原代码的子查询分开写。下面是一位朋友的提示,但具体如何实验不会: 如 $ids = xxx; 然后将 $ids 放入到 in($ids)中fetch 然后将得到的值用 implode连接起来 。

解决方案 »

  1.   

    DZ 怎么会禁止子查询呢?
    $ids = "";
    $query = DB::query("select max(id) from ".DB::table('favorites')." GROUP BY svip");
    while($row=DB::fetch($query))
    {
      $ids .= $row["id"].",";
    }
    if(!empty($ids)) 
    {
      $ids = substr($ids,0,strlen($ids)-1);  //去除最后一个逗号
      $favorites_query = DB::query("SELECT * FROM ".DB::table('favorites')."  WHERE ID IN (".$ids.") ORDER BY svupdatetime DESC LIMIT $start_limit, $perpage");
    }大概这样
      

  2.   


    [Type] 查询语句错误
    [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY svupdatetime DESC LIMIT 0, 20' at line 1
    [Query] SELECT * FROM favorites WHERE ID IN (,) ORDER BY svupdatetime DESC LIMIT 0, 20
      

  3.   

    $ids 没有值 
    在if(!empty($ids))  前 echo一下这个值 看下
    明天再回你
      

  4.   

    补齐之前的信息:
    Program messages:
    [Line: 0043]plugin.php(include)
    [Line: 0107]source\plugin\favorites\main.inc.php(DB::query)
      

  5.   


    在 if(!empty($ids))  前增加 echo "$ids"; 提示错误
    [Type] 查询语句错误
    [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') ORDER BY svupdatetime DESC LIMIT 0, 20' at line 1
    [Query] SELECT * FROM favorites WHERE ID IN (,) ORDER BY svupdatetime DESC LIMIT 0, 20如果删掉if(!empty($ids))  
    {
      $ids = substr($ids,0,strlen($ids)-1); //去除最后一个逗号
      $favorites_query = DB::query("SELECT * FROM ".DB::table('favorites')." WHERE ID IN (".$ids.") ORDER BY svupdatetime DESC LIMIT $start_limit, $perpage");
    }则正常,但没有返回数据。
      

  6.   

    1# 代码改为
    $ids = "";
    $query = DB::query("select max(id) as id from ".DB::table('favorites')." GROUP BY svip");
    while($row=DB::fetch($query))
    {
      $ids .= $row["id"].",";
    }
    if(!empty($ids))  
    {
      $ids = substr($ids,0,strlen($ids)-1); //去除最后一个逗号
      $favorites_query = DB::query("SELECT * FROM ".DB::table('favorites')." WHERE ID IN (".$ids.") ORDER BY svupdatetime DESC LIMIT $start_limit, $perpage");
    }