【问题标题】:max(id) and limit 10, but use them in different placesmax(id) 和 limit 10,但在不同的地方使用它们
【发布时间】:2012-09-02 09:05:58
【问题描述】:

我有两个表格,帖子和部分。我想获得最后 10 个帖子 WHERE section = 1, 但在不同的地方使用 10 个结果。我做一个函数:

     function sectionposts($section_id){
mysql_set_charset('utf8');
$maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 20";
$maxpost12 =mysql_query($maxpost1);
while ($maxpost_rows = mysql_fetch_array($maxpost12 ,MYSQL_BOTH)){
$maxpost2 = $maxpost_rows[0];
}
$query = "SELECT * FROM posts WHERE id = $maxpost2";
$query2 = mysql_query($query);
return $query2;
}
$query2 = sectionposts(6);
while ($rows = mysql_fetch_array($query2)){
echo $rows['title'] . "<br/>" . "<br/>";
echo $rows['id'] . "<br/>" . "<br/>";
echo $rows['image_section'] . "<br/>";
echo $rows['subject'] . "<br/>";
echo $rows['image_post'] . "<br/>";
}

这十个结果怎么能在不同的地方使用,并保持从一到十排列。

这是旧案例,我解决了,但我发现了另一个问题,如果客户删除了一个 id = 800 的帖子“所以 DB 中没有 id = 800”,所以当我得到最大 id减去$NUM,并且这个操作必须等于id = 800,所以我这里有一个编程错误,我该如何处理这样的事情。

    function getmax_id_with_minus ($minus){
    mysql_set_charset('utf8');
    $maxid ="SELECT max(id) FROM posts";
    $maxid1 =mysql_query($maxid);
        while ($maxid_row = mysql_fetch_array($maxid1)){
                $maxid_id = $maxid_row['0'];
                $maxid_minus = $maxid_id - $minus;
                }
    $selectedpost1 = "SELECT * FROM posts WHERE id = $maxid_minus";
    $query_selectedpost =mysql_query($selectedpost1);
        return ($query_selectedpost);

}
<?php 
$ss = getmax_id_with_minus (8);
while ($rows = mysql_fetch_assoc($ss)){
$main_post_1 = $rows;
?>

无论如何“真的”再次感谢:)!

【问题讨论】:

  • 你为什么使用mysql_query
  • 你有一些货物狂热的节目。您的 max() 查询只能返回一行,因为您没有使用 group-by 子句。因此,您的 while() 循环来获取结果是没有意义的。同样,代码很活泼 - 在您执行 max() 查询然后获取实际的最大记录之间,可以将另一条记录插入到数据库中。同样在 while 循环中显示结果 - 除非您出现多个重复的 ID,否则您只会得到一行,从而使 while() 循环毫无意义。

标签: php mysql arrays limit fetch


【解决方案1】:

关于发布代码的一些想法:

  • 首先,您应该停止使用 mysql_ 函数,因为它们是 deprecated 并且容易受到 SQL 注入。
$maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 20";
  • 当您 SELECT MAXMINCOUNTAVG ... 只返回单行的函数时,您不需要 ORDER BYLIMIT .

  • 鉴于您只要求MAX(id),您可以像这样组合查询来节省工作:

SELECT * FROM posts 
WHERE id = (SELECT MAX(id) from posts WHERE section_id = $section_id)

如果我理解你想要做什么(如果我错了,请纠正我),你的函数看起来像:

function sectionposts($section_id) {
    $link = mysqli_connect("localhost", "my_user", "my_password", "world");

    $stmt = mysqli_prepare($link, "SELECT title, id, image_section, subject, image_post FROM posts " 
        . "WHERE section_id = ? ORDER BY id DESC LIMIT 10");
    mysqli_stmt_bind_param($stmt, $section_id);
    return mysqli_query($link, $stmt)
}

$result = sectionposts(6);
while ($row = mysqli_fetch_assoc($result)) {
    echo $rows['title'] . "<br /><br />";
    echo $rows['id'] . "<br /><br />";
    echo $rows['image_section'] . "<br />";
    echo $rows['subject'] . "<br />";
    echo $rows['image_post'] . "<br />";
}

【讨论】:

    【解决方案2】:

    试试这个,为自己节省大量无意义的代码:

    $sql = "SELECT * FROM posts WHERE section_id=$section_id HAVING bar=MAX(bar);"
    $result = mysql_query($sql) or die(mysql_error());
    $row = mysql_fetch_assoc($result);
    echo ...;
    echo ...;
    

    having 子句可让您在单个操作中找到最大记录,而无需您的两个查询版本的固有竞争性。除非您允许具有相同 ID 的多条记录污染您的表,否则删除 while() 循环也会使事情变得更加清晰。

    【讨论】:

      【解决方案3】:

      似乎您想将它们存储在一个数组中。

       $posts = array(); //put this before the while loop. 
       $posts[] = $row; //put this in the while loop
      

      【讨论】:

      • 嗯,.. 没有让你好,你能不能再上飞机:) .
      • 函数会像这个函数 sectionposts($section_id){ mysql_set_charset('utf8'); $posts = 数组(); $maxpost1 ="SELECT max(id) from posts WHERE section_id = $section_id ORDER BY ID DESC LIMIT 10"; $maxpost12 =mysql_query($maxpost1); } $query2 = sectionposts(6);而 ($rows = mysql_fetch_array($query2)){ $posts[] = $rows;好的,但是我将如何获得结果?回声 $rows['title'];回声 $rows['id'];回声 $rows['image_section'];回声 $rows['subject'];回声 $rows['image_post'];谢谢迈克尔
      • @michael-wheeler 我不相信 OP 想要将结果放入数组中,因为他们正在获取结果集并使用 while 循环来显示它。
      猜你喜欢
      • 2010-11-12
      • 2018-12-19
      • 2014-06-18
      • 2016-04-17
      • 2023-03-16
      • 2010-09-30
      • 1970-01-01
      • 2016-04-04
      • 1970-01-01
      相关资源
      最近更新 更多