php - 使用 where in 条件和最新时间​​戳连接两个 MySQL 查询

标签 php mysql

第一个查询是针对选定的机构,第二个查询是为了获取对该机构的最新评论。

第一个查询:

SELECT 
    instituteId, 
    instituteName, 
    description 
FROM institutions 
WHERE instituteId IN ('1','2','3')";

第二个查询:

SELECT 
    name,
    review, 
    timestamp 
FROM reviews 
WHERE instituteId='1' 
ORDER BY timestamp DESC 
LIMIT 1; 

最佳答案

SELECT i.instituteId, 
       i.instituteName, 
       i.description ,
       r.name,
       r.review, 
       r.timestamp
FROM institutions i
INNER JOIN review r
ON i.instituteid = r.instituteId
INNER JOIN (SELECT instituteId,
                   MAX(timestamp) as timestamp
            FROM reviews
            GROUP BY instituteId 
            ) r1
ON r.instituteid = r1.instituteId 
AND r.timestamp = r1.timestamp
WHERE instituteId IN ('1','2','3')

关于php - 使用 where in 条件和最新时间​​戳连接两个 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49772424/

相关文章:

PHP 到 mySQL - 查找插入 ID 时复制条目

php - 根据用户输入编写包含变量 WHERE 的查询

MySQL In Clause 在列表具有重复元素且未排序的情况下的性能

mysql - SQL Server 到 MySQL 函数在值中使用撇号、引号和逗号

php - `$request->ajax()` 在Laravel中使用Fetch API拉取数据时没有检测到ajax请求

php - CakePHP 2.x - 动态切换数据库(针对整个应用程序)

php - 服务器花费太多时间来启动端口

php - E/ Volley : [1376] BasicNetwork. 执行请求 : Unexpected response code 404 for http://192. 168.0.109/connectphp/GetRestaurantData.php?restype=cafe

php - 包含表单 _REQUEST 的 PHP 插入语句上的 SQL 语法错误

python - 无法使用python的mysql.connector连接到mysql