簡體   English   中英

根據給定的排名和排序顯示mysql記錄

[英]Show mysql records according to the rank given and order by

我正在寫一個查詢,我應該根據給出的排名位置,價格順序和按站點名稱分組顯示記錄

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. 將記錄按站點名稱分組並按價格排序

最終預期產出

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

有什么最好的方法來執行此復雜的Query。

我很累這樣做,但並沒有達到我的預期。

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

這是一個有趣的問題。 我不太肯定我正確地理解了您,但是這是我想到的(有點時髦)。 它與您的樣本輸出匹配。

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM