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
qian88199496
V2EX  ›  MySQL

帮忙解释一个查询语句,谢谢,刚学的小白

  •  
  •   qian88199496 · Oct 15, 2017 · 4724 views
    This topic created in 3122 days ago, the information mentioned may be changed or developed.

    select b.brand,b.title,b.price from tb_item b where (select count(DISTINCT a.price) from tb_item a where b.brand = a.brand and b.price<=a.price)<=3 and b.brand is not null order by b.brand,b.price desc;

    表结构是这样的: mysql> desc tb_item; +------------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+--------------+------+-----+---------+----------------+ | id | bigint(20) | NO | PRI | NULL | auto_increment | | cid | bigint(20) | NO | MUL | NULL | | | brand | varchar(50) | YES | | NULL | | | model | varchar(50) | YES | | NULL | | | title | varchar(100) | YES | | NULL | | | sell_point | varchar(500) | YES | | NULL | | | price | bigint(20) | YES | | NULL | | | num | int(11) | YES | | NULL | | | barcode | varchar(30) | YES | | NULL | | | image | varchar(500) | YES | | NULL | | | status | tinyint(4) | YES | | 2 | | | created | datetime | YES | | NULL | | | updated | datetime | YES | | NULL | | +------------+--------------+------+-----+---------+----------------+ 13 rows in set (0.00 sec)

    8 replies    2017-10-16 16:27:48 +08:00
    megachweng
        1
    megachweng  
       Oct 15, 2017 via iPhone   ❤️ 1
    楼主可以考虑用 markdown 稍微拍一下版,连在一起就算有大佬知道,估计也没心情回复
    dongxinb
        2
    dongxinb  
       Oct 15, 2017
    粗粗看了一眼,好像是:
    将每个牌子的商品的价格放到一个 set 里面,然后取出前三大的。然后再输出这前三大的价格对应的商品牌子、名称、价格。
    vexe
        3
    vexe  
       Oct 15, 2017
    不会 markdown,可以贴到 gist
    flyingghost
        4
    flyingghost  
       Oct 16, 2017
    @megachweng @vexe
    入门三两天连 SQL 都看不明白的小白,你们居然指望掌握 md 和 gist。。。
    我看出来的是,这孩子连如何解决问题都没养成好习惯,怕不是才毕业吧。。。
    kylesean
        5
    kylesean  
       Oct 16, 2017
    兄弟,不行你截图啊。整这么费劲,看得人也费劲
    noNOno
        6
    noNOno  
       Oct 16, 2017
    查询降价三次以下的品牌的 brand,title,price,按 brand,price desc 排序
    业务逻辑是这个么
    bxb100
        7
    bxb100  
       Oct 16, 2017
    https://gist.github.com/bxb100/df6d3afe1d3b0df93be347875fd75fbd
    是查询一个牌子最高 3 个价格的信息吧
    scriptish
        8
    scriptish  
       Oct 16, 2017
    b.price<=a.price 这个语句可以怎么解释?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   964 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 18:11 · PVG 02:11 · LAX 11:11 · JFK 14:11
    ♥ Do have faith in what you're doing.