2015-06-19 49 views
0

我试图按列值排序结果。我有这个基本的查询:按列值获取SQL结果

SELECT `product_id`, 
     (SELECT `name` 
     FROM `specifications` s 
     WHERE `specification_id` = sp.`specification_id`) AS Specification, 
     `value` 
FROM `specs-product` sp 
WHERE `product_id` = '4' 
     AND (`specification_id` = '88' 
       OR `specification_id` = '103' 
       OR `specification_id` = '18' 
       OR `specification_id` = '15' 
       OR `specification_id` = '157' 
       OR `specification_id` = '89' 
       OR `specification_id` = '9' 
       OR `specification_id` = '223' 
       OR `specification_id` = '224' 
       OR `specification_id` = '29' 
       OR `specification_id` = '87' 
       OR `specification_id` = '219' 
       OR `specification_id` = '218' 
       OR `specification_id` = '220') 

我叫priorityspecifications已经列名。我需要得到由这个优先级值排序的结果。我试过了order by s.'priority'但是没有工作。我怎样才能做到这一点?

表结构如下:

specifications

+------------------+------+----------+ 
| specification_id | name | priority | 
+------------------+------+----------+ 
| 1    | abc |  5 | 
| 2    | xxxx |  2 | 
| 3 ababab   | 3 |   | 
+------------------+------+----------+ 

PRODUCTS TABLE

+------------+------------+--+ 
| product_id | reference | | 
+------------+------------+--+ 
|   1 | abc  | | 
|   2 | xxxx  | | 
|   3 | ababab  | | 
+------------+------------+--+ 
+0

你是什么意思它没有工作?什么数据类型是“优先级”列?如果它是一个字符串,它将按字典顺序排序,所以你必须把它作为一个整数。 – bernie

+0

它是'INT'。那么,我应该改变一些东西吗? – user1012181

+0

你可以给两个表的表结构吗? –

回答

1

添加

order by sp.'priority' 

假设我t在桌子上。如果这不起作用,我们需要你的模式。

编辑

SELECT `product_id`, 
s.name AS Specification, 
`value` 
FROM `specs-product` sp 
inner join `specifications` s on s.`specification_id` = sp.`specification_id` 
WHERE `product_id`='4' 
AND 
(s.`specification_id`='88' or 
s.`specification_id`='103' or 
s.`specification_id`='18' or 
s.`specification_id`='15' or 
s.`specification_id`='157' or 
s.`specification_id`='89' or 
s.`specification_id`='9' or 
s.`specification_id`='223' or 
s.`specification_id`='224' or 
s.`specification_id`='29' or 
s.`specification_id`='87' or 
s.`specification_id`='219' or 
s.`specification_id`='218' or 
s.`specification_id`='220') 
order by s.priority 
+0

'priority'是表格'规格'中的列 – user1012181

+0

使用正确答案编辑 –

+0

错误:'列'specification_id'在where子句中含糊不清' – user1012181