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

时间:2014-06-20 05:52:28

标签: php mysql

我正在撰写一个查询我应该根据按价格顺序给出的排名位置和按网站名称分组显示记录

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

我必须做的三个条件才能获得预期的输出 条件

  1. 如果site_priority!= 0,则将该记录放在该位置。说例如。如果site_priority = 5。在将所有网站名称分组后显示在第五位的记录
  2. 如果site_priority = 0
  3. ,则显示记录价​​格asc
  4. 按网站名称对记录进行分组,并按价格对其进行排序
  5. 最终预期产出

    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
    

    有没有最好的方法来执行这个复杂的查询。

    我厌倦了这样做,但它并没有像我预期的那样出现。

    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
    

    易于理解我的第一个条件

    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 个答案:

答案 0 :(得分:0)

这是一个有趣的解决方案。我并不积极,我理解正确,但这就是我想出来的(它有点时髦)。它匹配样本的输出。

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;