MySQL按两列排序

MySQL按两列排序,mysql,sql,database,Mysql,Sql,Database,我有一张像下面这样的桌子 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

我有一张像下面这样的桌子

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'); 创建表格产品(Product_id INT,ProductName VARCHAR(255), 特征枚举(“是”、“否”),优先级枚举(“p1”、“p2”、“p3”) 插入到产品中(产品名称、特色、优先级) 值(“产品A”、“是”、“p1”), (‘产品B’、‘否’、‘p2’), (‘产品C’、‘是’、‘p1’), (‘产品D’、‘否’、‘p1’), (‘产品E’、‘是’、‘p3’), (‘产品F’、‘否’、‘p2’), (‘产品G’、‘是’、‘p1’), (‘产品H’、‘是’、‘p2’), (‘产品一’、‘否’、‘p2’), (‘产品J’、‘是’、‘p3’), (‘产品K’、‘是’、‘p1’), (‘产品L’、‘否’、‘p3’); 我需要获得特色产品,然后是优先级为p1、p2和p3的产品

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 作品: ProdName |特色|优先级 产品A是p1 产品C是p1 产品G是p1 产品K是p1 产品H是p2 产品E是p3 产品J是p3 产品D编号p1 产品B编号p2 产品F编号p2 产品编号p2 产品编号p3 我在下面写了一个不起作用的查询

SELECT * FROM Products ORDER BY Featured IN ('Yes') desc, Priority IN ('p1', 'p2', 'p3') desc 选择* 来自产品 “是”描述中的订单特征, ('p1','p2','p3')描述中的优先级
你能找出其中的错误吗

为什么不简单地使用SQL作为:

 SELECT * 
 FROM Products
  ORDER BY Featured desc,
        Priority asc;
通过执行此操作,
Yes
将出现在
No
之前<代码>P1将出现在
P2
之前,而
P2
将出现在
P3
之前。我相信,这就是你想要的

如果订购时出现数据类型问题

 SELECT * 
 FROM Products
  ORDER BY CONCAT(Featured) desc,
        CONCAT(Priority) asc;
试试这个

Select * from Products ORDER BY Featured, Priority
如果在mysql enum上使用ORDER BY,它不会按字母顺序对其排序,而是按其在enum中的位置对其排序

如果要按描述的字母顺序排列,请强制转换枚举名称 像这样的线

Select * from Products ORDER BY  concat(Featured) desc , Priority 
检查此查询--

工作代码检查小提琴

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

这应该有效

您看到了什么错误?你所写的声明应该有效,约根德拉·辛格推荐的简化声明也应该有效。是否仅选择“产品标识”列?在您的示例中,它将全部为空。可能重复
SELECT * 
    FROM Products
where Featured IN ('Yes') and
      Priority IN ('p1', 'p2', 'p3')
Order by Featured asc,Priority,ProductName asc;