2012-10-30 16 views
10

ben Seçme öncelik p1, p2 ile ürünün ardından ürün ve p3MySQL al

 

Op: 
    ProdName | Featured | Priority 

    Product A Yes   p1 
    Product C Yes   p1 
    Product G Yes   p1 
    Product K Yes   p1 
    Product H Yes   p2 
    Product E Yes   p3 
    Product J Yes   p3 
    Product D No   p1 
    Product B No   p2 
    Product F No   p2 
    Product I No   p2 
    Product L No   p3 

almak gerekiyor
 

    CREATE TABLE Products(Product_id INT, ProductName VARCHAR(255), 
          Featured enum('Yes', 'No'), Priority enum('p1', 'p2', 'p3')) 


    INSERT INTO Products(ProductName, Featured, Priority) 
        VALUES('Product A', 'Yes', 'p1'), 
         ('Product B', 'No', 'p2'), 
         ('Product C', 'Yes', 'p1'), 
         ('Product D', 'No', 'p1'), 
         ('Product E', 'Yes', 'p3'), 
         ('Product F', 'No', 'p2'), 
         ('Product G', 'Yes', 'p1'), 
         ('Product H', 'Yes', 'p2'), 
         ('Product I', 'No', 'p2'), 
         ('Product J', 'Yes', 'p3'), 
         ('Product K', 'Yes', 'p1'), 
         ('Product L', 'No', 'p3'); 


Aşağıda

aşağıda bir sorgu yazdım bir tablo gibi sahip hangi çalışmıyor ..

           
    SELECT * 
    FROM Products 
    ORDER BY Featured IN ('Yes') desc, 
      Priority IN ('p1', 'p2', 'p3') desc 

olabilir o

u plz nokta hata No önce görünür bu Yes yaparak

SELECT * 
FROM Products 
    ORDER BY Featured desc, 
     Priority asc; 

:

+0

Hangi hatayı görüyorsunuz? Yazdığınız gibi deyim işe yaramalıdır, ans böylece Yogendra Singh'in önerdiği sadeleştirilmiş olmalıdır. Sadece Product_id sütunu seçiyor musunuz? Örneğinizde bütün NULL'lar olacak. – Yuri

+0

Olası iki kopyası [PHP MySQL Order by Two Columns] (http://stackoverflow.com/questions/514943/php-mysql-order-by-two-columns) –

cevap

13

Eğer mysql üzerinde SİPARİŞ BY kullanırsanız bu

Select * from Products ORDER BY Featured, Priority 

alfabetik tarafından sipariş olmaz enum deneyin ama bunu sipariş olacaksa onun numaradaki konum. Bu

Select * from Products ORDER BY concat(Featured) desc , Priority 
4

Neden sadece olarak SQL kullanmayın. P1P3 önce P2 ve P2 önce görünür. İnanıyorum, istediğin bu.

sonra sipariş veri tipi sorunu

SELECT * 
FROM Products 
    ORDER BY CONCAT(Featured) desc, 
     CONCAT(Priority) asc; 
+0

Hayır, 2 işe gitmeyecek – aprilleocean

0

kontrol edin gibi bir dizeye enum adını dökme açıklayan gibi alfabetik sipariş etmek isterseniz

bu

SELECT * 
FROM Products 
    ORDER BY Featured asc,Priority,ProductName asc; 

Fiddle

Çalışma kod denetimi query-- keman

-1
SELECT * 
    FROM Products 
where Featured IN ('Yes') and 
     Priority IN ('p1', 'p2', 'p3') 
Order by Featured asc,Priority,ProductName asc; 

Çalışması gereken