dongzong2017 2014-06-20 05:52
浏览 48

根据给定的排名和顺序显示mysql记录

I am writing a query i should show the records according to the rank position given along with price order and group by sitename

shopdetails

id | productid | productname | sitename | siteid | site_priority | price | color
1     555          xyz          a          1          0            10      blue  
2     555          xyz          b          12         1            50      blue  
3     555          xyz          a          1          0            12      red  
4     555          xyz          c          3          4            9       red
5     555          xyz          e          15         5            19      blue
6     555          xyz          e          15         5            21      red
7     555          xyz          b          12         1            42      red 
8     555          xyz          c          3          4            56      blue

Three conditions i have to do for getting expected output Conditions

  1. if site_priority !=0 then put that record in that position. Say Eg. if the site_priority = 5 . show that record in fifth position after grouping all the sitename
  2. Show records price asc if site_priority = 0
  3. group the records by sitename and sort them by price

Final Expected Output

id | productid | productname | sitename | siteid | site_priority | price | color
  7     555          xyz           b        12           1            42    red   
  2     555          xyz           b        12           1            50    blue

############ the above two records are kept in the First Position since site_priority = 1 and ordered by price asc    
Now check for site_priority 2 is there if not show site_priority = 0 by price asc  ,
Now 2nd records would be

  1     555          xyz          a          1          0            10      blue 
  3     555          xyz          a          1          0            12      blue 

 Now check for site_priority 3 is there if not check for site_priority 0 ,
 is not then make the priority one level minus .
 move site_priority 4 to 3 , 5 to 4 .

  4     555          xyz          c          3          4            9       red
  8     555          xyz          c          3          4            56      blue
  5     555          xyz          e          15         5            19      blue
  6     555          xyz          e          15         5            21      red

Is there any best way to do this complicated Query .

i tired doing like this but it is not coming as i expected.

select 
    productid,
    productname,
    sitename,
    site_priority,
    price,
    colorname,
    (select 
            count(*)
        from
            shopdetails b
        where
            productid = 1250 and b.site_priority > a.site_priority order by price asc)+1  as rnk
from
    shopdetails a
where
    productid = 1250
having site_priority > 0
order by rnk

Easy to understand my first condition

sitename | priority
a            1
b            2  
c            3
d            0  
e            0
f            0
g            0
h            0
i            5



outpt

a  1 ==> position 1
b  2 ==> position 2
c  3 ==> position 3
d  0 ==> position 4
i  5 ==> position 5
e  0 ==> position 6
f  0 ==> position 7
g  0 ==> position 8
h  0 ==> position 9
  • 写回答

1条回答 默认 最新

  • duanfu9523 2014-06-20 08:51
    关注

    This was an interesting one to solve. I'm not positive I understood you correctly but here's what I came up with (it's a little funky). It matches the output of your sample though.

    SELECT * FROM test
    ORDER BY CASE WHEN site_priority = 0 THEN (
        SELECT k.outp FROM (
            SELECT @rownum:= @rownum + 1 outp, t.site_priority outp2 FROM (
                SELECT DISTINCT site_priority FROM test ORDER BY site_priority ASC) t, 
                (SELECT @rownum := 0) r 
                WHERE t.site_priority != @rownum) k 
        WHERE k.outp != k.outp2 limit 1)
    ELSE site_priority END, sitename, price;
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度