duanhe1903 2018-03-04 13:38
浏览 90
已采纳

显示基于百分比的Sql结果

Good Day Guys! I am trying to get a desired result in SQL.

I have a Table like this

Products | Colors
******************
Bag      | Blue
Shoes    | Blue
Shirt    | Yellow
Sandal   | Yellow
Phone    | Yellow
Cotton   | Blue
Paper    | Blue
Biro     | Blue
Track    | Yellow
Pouch    | Yellow
Cap      | Yellow
Hat      | Yellow
Pants    | Blue
Bucket   | Blue
Belt     | Blue
Car      | Yellow
Socks    | Yellow

The rows above is 20.

What I want to achieve is to display 10 results at random:

where: 3 rows (30% of the result) are blue and 7 rows (70% of the result) are yellow.

Thank you. I apologize for not asking with details.

  • 写回答

1条回答 默认 最新

  • doonbfez815298 2018-03-04 14:30
    关注

    Here's a method that almost does what you want. To achieve your exact specification in pure SQL is quite a lot more involved...

    DROP TABLE IF EXISTS my_table;
    
    CREATE TABLE my_table
    (Products VARCHAR(20) NOT NULL
    ,Colors VARCHAR(20) NOT NULL
    ,PRIMARY KEY(products,colors)
    );
    
    INSERT INTO my_table VALUES
    ('Bag','Blue'),
    ('Shoes','Blue'),
    ('Shirt','Yellow'),
    ('Sandal','Yellow'),
    ('Phone','Yellow'),
    ('Cotton','Blue'),
    ('Paper','Blue'),
    ('Biro','Blue'),
    ('Track','Yellow'),
    ('Pouch','Yellow'),
    ('Cap','Yellow'),
    ('Hat','Yellow'),
    ('Pants','Blue'),
    ('Bucket','Blue'),
    ('Belt','Blue'),
    ('Car','Yellow'),
    ('Socks','Yellow');
    
    SELECT * FROM my_table ORDER BY CASE WHEN colors = 'blue' THEN RAND()*3 ELSE RAND()*7 END DESC LIMIT 10;
    +----------+--------+
    | Products | Colors |
    +----------+--------+
    | Socks    | Yellow |
    | Shirt    | Yellow |
    | Hat      | Yellow |
    | Track    | Yellow |
    | Cap      | Yellow |
    | Sandal   | Yellow |
    | Car      | Yellow |
    | Pants    | Blue   |
    | Shoes    | Blue   |
    | Belt     | Blue   |
    +----------+--------+
    
    SELECT * FROM my_table ORDER BY CASE WHEN colors = 'blue' THEN RAND()*3 ELSE RAND()*7 END DESC LIMIT 10;
    +----------+--------+
    | Products | Colors |
    +----------+--------+
    | Track    | Yellow |
    | Phone    | Yellow |
    | Hat      | Yellow |
    | Socks    | Yellow |
    | Cap      | Yellow |
    | Sandal   | Yellow |
    | Shoes    | Blue   |
    | Biro     | Blue   |
    | Pouch    | Yellow |
    | Bucket   | Blue   |
    +----------+--------+
    
    SELECT * FROM my_table ORDER BY CASE WHEN colors = 'blue' THEN RAND()*3 ELSE RAND()*7 END DESC LIMIT 10;
    +----------+--------+
    | Products | Colors |
    +----------+--------+
    | Sandal   | Yellow |
    | Hat      | Yellow |
    | Pouch    | Yellow |
    | Socks    | Yellow |
    | Cap      | Yellow |
    | Bucket   | Blue   |
    | Paper    | Blue   |
    | Phone    | Yellow |
    | Track    | Yellow |
    | Shirt    | Yellow |
    +----------+--------+
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理