V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
kaiki
V2EX  ›  MySQL

求一个多条件查询,每个条件都能查询指定数量的结果的语句

  •  
  •   kaiki · Nov 15, 2022 · 2145 views
    This topic created in 1270 days ago, the information mentioned may be changed or developed.

    如:从 group 为 1 、2 、3 的条件中查询 point 前 3 的项目的数据,这样就可以查询出 9 条数据了,能一条语句搞定吗?

    9 replies    2022-11-16 00:28:25 +08:00
    ackoly
        1
    ackoly  
       Nov 15, 2022 via iPhone
    什么数据库,分析函数了解一下
    optional
        2
    optional  
       Nov 15, 2022
    row_number over partition by 一下,再套个子查询或者 cte ,完事
    wolfie
        3
    wolfie  
       Nov 15, 2022   ❤️ 1
    kwh
        4
    kwh  
       Nov 15, 2022   ❤️ 1
    select * from table where group=1 order by point desc limit 3
    union
    select * from table where group=2 order by point desc limit 3
    union
    select * from table where group=3 order by point desc limit 3
    dqzcwxb
        5
    dqzcwxb  
       Nov 15, 2022
    并行查三次是最终答案
    wxf666
        6
    wxf666  
       Nov 15, 2022
    @dqzcwxb 请教一下,为嘛不选用窗口函数呢?(差在哪儿?)
    bugfan
        7
    bugfan  
       Nov 15, 2022
    老哥这头像真帅~~
    kaiki
        8
    kaiki  
    OP
       Nov 15, 2022
    @wolfie 感谢,文章很有帮助
    @kwh 合并查询的确可以,谢谢
    dqzcwxb
        9
    dqzcwxb  
       Nov 16, 2022
    @wxf666 #6 没法做缓存,把性能压力交给数据库不是一个能长久的方案
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5900 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 03:13 · PVG 11:13 · LAX 20:13 · JFK 23:13
    ♥ Do have faith in what you're doing.