php - MySQL 根据之前的查询选择行

标签 php mysql

$clicks = mysqli_real_escape_string($conn,$_POST['clicks']);
$modifier = 10*$clicks;

$result = mysqli_query($conn,"SELECT * FROM posts ORDER BY freshness LIMIT $modifier,10");

while($row = mysqli_fetch_assoc($result)){
    $posts[] = $row;
}

$result2 = mysqli_query($conn,"SELECT * FROM comments ORDER BY time WHERE ????");

while($row2 = mysqli_fetch_assoc($result2)){
    $comments[] = $row2;
}

对于 $result2,如何选择 10 个帖子 ID 中每一个的 WHERE(即 $row['id'])来自$结果?否则我会不必要地选择表中的每一条评论。

最佳答案

为什么不在一个查询中完成这一切?

 $modifier = 10*$clicks;

 $query = "SELECT *
    FROM posts 
    INNER JOIN comments
    ON posts.id = comments.post_id
    ORDER BY posts.freshness 
    LIMIT '$modifier'";

关于php - MySQL 根据之前的查询选择行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30699305/

相关文章:

php - Codeigniter/Eloquent 选择但不插入

php - 使用 php 进入 mysql 内部的部分条目

php - 如何将从 MySQL 检索到的数据插入到 Android 应用程序中的数组中?

php - 执行脚本

java - 数据库到android,用php

javascript - 计算 Javascript 时间与百分比之间的时间

javascript - 将本地存储的图像共享到 Facebook 墙

mysql - Laravel 的 ORM 如何指定数据的排序值?

mysql - 地址簿 : Turn an "at least one list suffices" query to an "all lists need to be present"

java - 插入 MySQL Java