나는 우선 순위와 가격으로 주문해야하는 mysql 쿼리를 작성하고있다. 여기 내 테이블 구조가 있습니다.MySQL 복수 주문
TABLE1
------
id | name | priority_order
1 A 1
2 B 2
3 C 0
4 D 0
5 E 4
6 F 0
7 G 0
8 H 3
9 I 5
10 J 0
TABLE2
------
id | productid | listname | Color | price
100 5012 A Blue 700
101 5012 B Blue 400
102 " C Blue 850
103 " A Red 650
104 " H Red 550
105 " B Red 600
106 " E Green 780
107 " E Blue 650
108 " F Blue 300
109 " G Red 355
110 " B Black 550
111 " A Black 480
112 " C Red 800
113 " H Black 785
114 " I Black 625
115 " I Red 480
이제 최저 가격으로 주문해야합니다. 즉 우선 순위가 낮은 가격, 우선 순위가 낮은 가격, 우선 순위가 낮은 가격을 표시해야합니다. 나머지 가격은 자신의 위치에 있습니다. 내가 시도
Expected Output :
----------------
id | productid | listname | Color | price | priority_order
111 5012 A Black 480 1
101 5012 B Blue 400 2
104 " H Red 550 3
106 " E Blue 650 4
115 " I Red 480 5
=========== The above order is taken low price in each priority ========
=========== Now the remaining should be in any priority order but should be in low price ASC====
108 " F Blue 300 0
109 " G Red 355 0
110 " B Black 550 2
105 " B Red 600 2
114 " I Black 625 5
..................... and so on..
쿼리는
select a.name , a.priority_order , b.color , b.productid , b.price from table1 a inner join table2 b on a.name = b.listname where productid = 5012 order by
case when priority_order = 0 then 2
when priority_order > 0 then 1
b.price ASC
주어진 Null이 아닌 우선 순위 목록에 대해 가장 낮은 가격을 찾으려면 GROUP BY와 함께 하위 쿼리를 사용해야합니다. 이것은 당신이 다루려고하는 사소한 문제에서 멀리 떨어져 있습니다. –
나는 각 우선 순위의 최소 가격만을 시도해 보았다. 'a.name = a.priority_order, b.color, b.productid, min (b.price)을 가격으로 선택하십시오. a.name = b.listname의 table1 a inner join table2 b에서 productid = 5012 abd priority_order> 0 group by a.name order by order_order 오름차순, b.price 오름차순 – Raaga