从起始值开始的MySQL顺序 [英] MySQL Order from a starting value

查看:109
本文介绍了从起始值开始的MySQL顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在产品页面上,我有一个下拉列表,列出了与产品页面关联的当前颜色选项.

On a product page I have a dropdown that lists current colour options associated to product page.

在此示例中,产品页面的SKU为250E,可在以下位置使用:

In this example, the product page SKU is 250E and it available in:

GREEN
BLACK

如果客户选择GREEN,那么我想运行一个MySQL命令,该命令将根据下面显示的custom_order值将数据更改为首先显示GREEN值.

If the customer selects GREEN, then I want to run a MySQL command that will change the data to show GREEN values first based in the custom_order value shown below.

起始值应覆盖其他数据项,然后应保留custom_order值. custom_order字段的字母类似c1,c2(它们将始终在底部)

The start value should overide the other data items and then it should retain custom_order values. The custom_order field has letters like c1, c2 (they will always be at the bottom)

Colour Table
============
ID      COLOURID        NAME
-------------------------------------
2           5           BLACK
3           6           GREEN


Product Table
=============
ID      SKU         PICTURE URL             COLOURID            CUSTOM_ORDER        
-----------------------------------------------------------------
22      250E        cdn_hash_1.jpg          5                   1
23      250E        cdn_hash_2.jpg          5                   2
24      250E        cdn_hash_3.jpg          5                   3
225     250E        cdn_hash_4.jpg          5                   4
226     250E        cdn_hash_5.jpg          6                   5
227     250E        cdn_hash_6.jpg          6                   6
325     250E        cdn_hash_c1.jpg         -                   c1
426     250E        cdn_hash_c2.jpg         -                   c2
527     250E        cdn_hash_c3.jpg         -                   c3

选择*从按custom_order DESC排序的产品中

现在,我要执行以下操作:

Now, I want to do the following:

选择*从按custom_order和START VALUE ='6'排序的产品中

因此,无论如何,c1,c2保持不变,结果将是:

Therefore, no matter what, the c1, c2 remain intact, result would be:

22      250E        cdn_hash_5.jpg          6                   5
23      250E        cdn_hash_6.jpg          6                   6
24      250E        cdn_hash_1.jpg          5                   1
225     250E        cdn_hash_2.jpg          5                   2
226     250E        cdn_hash_3.jpg          5                   3
227     250E        cdn_hash_4.jpg          5                   4
325     250E        cdn_hash_c1.jpg         -                   c1
426     250E        cdn_hash_c2.jpg         -                   c2
527     250E        cdn_hash_c3.jpg         -                   c3

推荐答案

这是我一直在寻找的答案.我希望我不会为此而感到沮丧,但我设法解决了这个问题.

This is the answer I was looking for. I hope I am not downvoted for this but I managed to resolve this.

让我再次解释一些更简单的术语. MySQL ORDER BY +开始于..

Let me explain again simpler terms. MySQL ORDER BY + Start with..

MySQL:

id | name  |
------------
1  | Joe   |
2  | Craig |
3  | Shawn |
4  | Ryan  |
5  | Seth  |

PHP:

$a = mysql_query("SELECT * FROM table_name ORDER BY name DESC");

我想做的是,我想从id:3开始,所以它应该输出:

what I want to do though is, I want to start at id: 3, so it should output:

3,4,5,1,2

解决方案1 ​​

SELECT id, name
FROM table_name
ORDER BY id < 3, id

结果:

3  Shawn
4  Ryan
5  Seth
1  Joe
2  Craig

解决方案2

您可以使用FIELD,例如

SELECT * 
FROM products 
ORDER BY FIELD(`order`, 6) ASC

这篇关于从起始值开始的MySQL顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆