文章详情页
mysql连表排序
浏览:200日期:2022-06-20 17:57:13
问题描述
表Aid info 1message12message23message3表Bid goods_id1 11 22 33 4
AB表id连表,查询结果根据B表的相同id个数排序,例如id=1的在B表有两个,排在前面,id=2和id=3的只有一个,排在后面,请问mysql排序语句order by该怎么写?
问题解答
回答1:select A.id, A.info, count(B.goods_id) from A inner join B on A.id = B.id group by A.id order by count(B.goods_id) desc回答2:
说一下可用的sql语句,性能上不是太好,多了一次对表b的查询。
select a.*, b.*from a inner join b on a.id = b.id inner join ( select id, count(*) as cnt from b group by id ) c on a.id = c.idorder by c.cnt, a.id回答3:
假如数据量会多的话。要是我,就重新考虑需求的合理性。能推掉就推掉。推不掉就在,a表加多一个冗余字段 goods_count。然后建联合索引。
相关文章:
1. python相关问题求解决,有偿2. javascript - vue2.0中使用vue2-dropzone的demo,vue2-dropzone的github网址是什么??百度不到。3. jquery - css3 scale 缩放图片问题4. javascript - 怎么用flatlist替换scrollview?5. android - 安卓activity无法填充屏幕6. html5 - iOS的webview加载出来的H5网页,怎么修改html标签select的样式字体?7. php - 数据库存图片,是存图片名称?还是存图片路径??8. javascript - 怎么实现点击表格中的某一行然后就在表单处出现表格中的对应的属性值啊9. css - 关于background-position百分比的问题?10. html - css部分position fixed web网页问题 纯css做的下拉
排行榜

网公网安备