mysql多表查询及其 group by 组内排序
//多表查询:得到最新的数据后再执行多表查询 SELECT *FROM `students` `st` RIGHT JOIN(
SELECT * FROM
(
SELECT * FROM goutong WHERE goutongs='asdf' ORDER BY time DESC
) AS gtt GROUP BY gtt.name_id ORDER BY gtt.goutong_time DESC ) gt
ON `gt`.`name_id`=`st`.`id` LIMIT 10
//先按时间排序查询,然后分组(GROUP BY )
SELECT * FROM
(
SELECT * FROM goutong WHERE goutongs='asdf' ORDER BY time DESC ) AS gtt GROUP BY gtt.name_id ORDER BY gtt.time DESC
参考:http://blog.csdn.net/shellching/article/details/8292338
有数据表 comments
------------------------------------------------
| id | newsID | comment |
theTime |
------------------------------------------------
| 1 | 1 | aaa | 11 |
------------------------------------------------
| 2 | 1 | bbb | 12 |
------------------------------------------------
| 3 | 2 | ccc | 12 |
------------------------------------------------
newsID是新闻ID,每条新闻有多条评论comment,theTime是发表评论的时间
现在想要查看每条新闻的最新一条评论:
select * from comments group by newsID 显然不行
select * from comments group by newsID order by theTime desc
是组外排序,也不行
下面有两种方法可以实现:
(1)
selet tt.id,tt.newsID,tt.comment,tt.theTime from(
select id,newsID,comment,theTime from comments order by theTime desc) as tt group by newsID
(2)
select id,newsID,comment,theTime from comments as tt group by id,newsID,comment,theTime having
theTime=(select max(theTime) from comments where newsID=tt.newsID)
补充: 通过最大时间 然后再联合查询出其它信息,实现避免分组排序的问题。(多个子查询实现功能)
'SELECT gt.time, gt.name_id, gt.goutong,gt.operator, st.id,st.Stu_name,st.Stu_sex,st.stu_gongsi,st.stu_waishangke,st.Stu_jjcourse,st.Stu_phone,st.Stu_beizhu FROM jingjie_students AS st RIGHT JOIN (SELECT A.* FROM jingjie_goutong A, (SELECT name_id,MAX(goutong_time) goutong_time FROM jingjie_goutong WHERE operator = '小明' GROUP BY name_id) B WHERE A.name_id = B.name_id AND A.time = B.time ORDER BY A.time DESC LIMIT ,) gt ON st.id = gt.name_id';
mysql多表查询及其 group by 组内排序的更多相关文章
- mysql多表查询,group by并将结果导出来csv文件
SQL手动操作得少,遇到一个CASE,就记录一个CASE吧. 今天遇到的是统计一个发布结果. 这个发布表中,有两个字段是外键,并且要求按其中一个外键,作group by操作, 最后,导出来excel能 ...
- Mysql 单表查询-排序-分页-group by初识
Mysql 单表查询-排序-分页-group by初识 对于select 来说, 分组聚合(((group by; aggregation), 排序 (order by** ), 分页查询 (limi ...
- Mysql 单表查询 子查询 关联查询
数据准备: ## 学院表create table department( d_id int primary key auto_increment, d_name varchar(20) not nul ...
- python 3 mysql 单表查询
python 3 mysql 单表查询 1.准备表 company.employee 员工id id int 姓名 emp_name varchar 性别 sex enum 年龄 age int 入职 ...
- mysql group by组内排序
mysql group by组内排序: 首先是组外排序: SELECT z.create_time,z.invoice_id from qf_invoice_log z where z ...
- python3 mysql 多表查询
python3 mysql 多表查询 一.准备表 创建二张表: company.employee company.department #建表 create table department( id ...
- mysql 不同版本下 group by 组内排序的差异
最近发现网上找的 group by 组内排序语句在不同的mysql版本中结果不一样. 建表语句: SET FOREIGN_KEY_CHECKS=0; -- ---------------- ...
- Mysql 单表查询where初识
Mysql 单表查询where初识 准备数据 -- 创建测试库 -- drop database if exists student_db; create database student_db ch ...
- MySQL多表查询合并结果union all,内连接查询
MySQL多表查询合并结果和内连接查询 1.使用union和union all合并两个查询结果:select 字段名 from tablename1 union select 字段名 from tab ...
随机推荐
- 记一次在线安装postgresql-9.4的问题
一.在线安装 apt-get install postgresql-9.4 二.运行状态 systemctl status postgresql 显示运行正常,但是却没有启动默认的5432监听端口 三 ...
- Hibernate 连接访问多个数据库(含访问不同数据库的相同表)(转)
利用hibernate访问不同数据库中的不同表或不同数据库中的相同表. 本人在开发过程中的解决方案,希望大家交流.一般用myEclipse工具会自动生成Hibernate的相关文件,大致有下面几类: ...
- jenkins启动appium服务
想在jenkins中,自动定时启动appium服务,shell命令已准备如下: BUILD_ID=dontKillMe echo "" > appium.log nohup ...
- 【转】WARNING! File system needs to be upgraded. You have version null and I want version 7. Run the '${HBASE_HOME}/bin/hbase migrate' script. 的解决办法
前段时间集群出问题,hadoop和hbase启动不了了. 后来hadoop回复了,hbase死活master无法启动.打开日志发现报了以下错误: WARNING! File system needs ...
- jQueryWEUI自定义对话框-带有textarea
jQueryWEUI 示例下载 在jQueryWEUI中提供了很多类型的对话框, 可以去访问看一下. 今天记录的则是,自己定义的一个带有文本域的对话框,这样,可以不通过调转页面,实现一些信息的提交. ...
- highlightjs 详解
起源: 最近想做一个代码高亮的功能.发现开源社区已经有了这类的项目.比如说highlightjs. 第一步:下载highlightjs 官网:https://highlightjs.org 可以看到它 ...
- 使用mysqltools配置读写分离环境
mysqltools 目前自带了cat的的自动化安装功能 一.环境规划: 主机名 ip地址 角色 mtls17 10.186.19.17 master mtls19 10.186.19.19 slav ...
- php反射获取类和方法中的注释
通过php中的反射机制,获取该类的文档注释,再通过获取其所有的方法,获取方法的注释 所用到的主要类及其方法 ReflectionClass ReflectionClass::getDocComment ...
- [LeetCode] Shortest Word Distance I & II & III
Shortest Word Distance Given a list of words and two words word1 and word2, return the shortest dist ...
- Windows 虚拟桌面工具 Desktops v2.0
https://technet.microsoft.com/en-us/sysinternals/cc817881.aspx