dongshengheng1013 2013-10-02 11:01
浏览 52
已采纳

MySQL:在select查询中获取多个值的Porportionate值

This is the table i got with table name photos:

photo_id   user_id
401           1     
403           1     
405           1 
407           2     
408           1     
409           2      
410           1     
411           3      
412           2          
413           2      
420           2      
423           2

I am Currently Using the Query

SELECT *
FROM photos
WHERE user_id
IN ( 1, 2 )
LIMIT 0 , 6

Ids inside IN can have multiple values not just two

What I Get Is

photo_id   user_id
401           1     
403           1     
405           1 
407           2     
408           1     
409           2     

Without Having Proportionate values for each user_id

What I Want is Get Values in Equal Proportion from both UserIds. That is if fetching limit is 6 then i need 3 values or less than 3 if less than 3 elements exists from 1 and 3 values or less than 3 if less than 3 elements exists from 2

And If there are 3 user ids and fetching limit is 6; All three ids will have result with value of 2

photo_id   user_id
401           1     
403           1     
405           1 
407           2     
409           2      
412           2     

I can Do it in Multiple queries ; But Is there any way to do it in single query;

  • 写回答

1条回答 默认 最新

  • dqsp60748 2013-10-02 11:11
    关注

    Consider the following...

    DROP TABLE IF EXISTS photos;
    
    CREATE TABLE photos
    (photo_id   INT NOT NULL PRIMARY KEY
    ,user_id INT NOT NULL
    );
    
    INSERT INTO photos VALUES
    (401           ,1),     
    (403           ,1),     
    (405           ,1),     
    (407           ,2),     
    (408           ,1),     
    (409           ,2),     
    (410           ,1),     
    (411           ,3),     
    (412           ,2),     
    (413           ,2),     
    (420           ,2),     
    (423           ,2);
    
    SELECT x.*
         , COUNT(*) rank 
      FROM photos x 
      JOIN photos y 
        ON y.user_id = x.user_id 
       AND y.photo_id <= x.photo_id 
     GROUP 
        BY x.photo_id
         , x.user_id 
     ORDER 
        BY user_id
         , rank;
    +----------+---------+------+
    | photo_id | user_id | rank |
    +----------+---------+------+
    |      401 |       1 |    1 |
    |      403 |       1 |    2 |
    |      405 |       1 |    3 |
    |      408 |       1 |    4 |
    |      410 |       1 |    5 |
    |      407 |       2 |    1 |
    |      409 |       2 |    2 |
    |      412 |       2 |    3 |
    |      413 |       2 |    4 |
    |      420 |       2 |    5 |
    |      423 |       2 |    6 |
    |      411 |       3 |    1 |
    +----------+---------+------+
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算