0

ABの2 つのテーブルがあります。私の質問は-

Select A.item, B.description, B.brand from A inner Join B on A.id=B.a_id where B.brand_id=1 limit 0,3
UNION
Select A.item, B.description, B.brand from A inner Join B on A.id=B.a_id where B.brand_id=2 limit 0,3
UNION
Select A.item, B.description, B.brand from A inner Join B on A.id=B.a_id where B.brand_id=3 limit 0,3

出力は次のようになります-

item    description    brand
1001    item1          brand1
1002    item2          brand1
1003    item3          brand1
1004    item4          brand2
1005    item5          brand2
1006    item6          brand2
1007    item7          brand3
1008    item8          brand3
1009    item9          brand3

今、私の要件は、レコードを次のように取得することです-

item    description    brand
1001    item1          brand1
1004    item4          brand2
1007    item7          brand3
1002    item2          brand1
1005    item5          brand2
1008    item8          brand3
1003    item3          brand1
1006    item6          brand2
1009    item9          brand3

なにか提案を :(

4

1 に答える 1

3

純粋な SQL の回答が必要な場合は、次の Oracle のRANK OVER PARTITIONに対する MySQL の回避策をインライン ビューと組み合わせて使用​​し、ブランドの数に関係なく順序付けを行う必要があります。

select item,description,brand
from
(select A.item, B.description, B.brand,
case B.brand
        when @curBrand 
        then @curRow := @curRow + 1 
        else @curRow := 1 and @curBrand := B.brand END
      as rank
from A inner join B on A.id=B.a_id
join (select @curRow := 0, @curBrand := '') r
) t
order by t.rank,t.brand;

楽しみ!

于 2013-02-15T16:34:06.663 に答える