操纵ORDER BY [英] manipulating ORDER BY

查看:97
本文介绍了操纵ORDER BY的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一张桌子,说


----------

ID | myvalue

----------

1 | B

2 | C

3 | A

4 | A

5 | B

6 | C

- --------


是否可以执行SELECT语句,以便输出按列myvalue排序

。由B,然后是A,然后是C?


有些像:

SELECT * FROM mytable ORDER BY myvalue(''B'',''A' ','''C'');


这只是一个例子,但我想知道这个原则是否可能是



谢谢,

巴特

解决方案

Bart Van der Donck写道:

你好,

我有一张桌子,说

----------
ID | myvalue
- --------
1 | B
2 | C
3 | A
4 | A
5 | B
6 | C
----------

是否可以执行SELECT语句,以便按列myvalue排序输出
。由B,然后A,然后是C?

有些像:
SELECT * FROM mytable ORDER BY myvalue(''''',''A'',''C'') ;




一种方法是创建另一个表,在那里你告诉我们应该以什么顺序值



table myvalue_order

----------------------

order_number |价值

1 | B

2 | A

3 | C $ / $
----------------------


然后使用加入,比如这个:


选择mytable。*来自mytable,value_order

其中mytable.myvalue = value_order.value

by myvalue_order。 order_number;


请注意,如果使用这样的查询,则根本不显示使用

order_number的值的行。如果你想获得那些

值,你可以使用:


选择mytable。*来自mytable

左外连接mytable.myvalue = value_order.value上的value_order

by myvalue_order.order_number;


但是这会在第一个位置放置没有订单号的行。你

当然可以使用相反的订单号


table myvalue_order

------------ ----------

order_number |价值

3 | B

2 | A

1 | C $ / $
----------------------


然后再做...订单by order_number desc;


空值应该在列表的底部。


Bart Van der Donck写道:< blockquote class =post_quotes>你好,

我有一张桌子,说

----------
ID | myvalue
----------
1 | B
2 | C
3 | A
4 | A
5 | B 6 | C
----------

是否可以执行SELECT语句,以便输出按列myvalue排序。由B,然后A,然后是C?

有些像:
SELECT * FROM mytable ORDER BY myvalue(''''',''A'',''C'') ;




一种方法是创建另一个表,在那里你告诉我们应该以什么顺序值



table myvalue_order

----------------------

order_number |价值

1 | B

2 | A

3 | C $ / $
----------------------


然后使用加入,比如这个:


选择mytable。*来自mytable,value_order

其中mytable.myvalue = value_order.value

by myvalue_order。 order_number;


请注意,如果使用这样的查询,则根本不显示使用

order_number的值的行。如果你想获得那些

值,你可以使用:


选择mytable。*来自mytable

左外连接mytable.myvalue = value_order.value上的value_order

by myvalue_order.order_number;


但是这会在第一个位置放置没有订单号的行。你

当然可以使用相反的订单号


table myvalue_order

------------ ----------

order_number |价值

3 | B

2 | A

1 | C $ / $
----------------------


然后再做...订单by order_number desc;


空值应位于列表的底部。


" Bart Van der Donck" < BA ** @ nijlen.com>在消息中写道

ID | myvalue
----------
1 | B
2 | C
3 | A
4 | A
5 | B
6 | C
----------

是否有可能执行一个SELECT语句,以便输出按列myvalue排序。 by B,然后是A,然后是C?




是否


SELECT * FROM mytable ORDER BY if(myvalue ='' A',2,......);


工作?


Hello,

I have a table, say

----------
ID|myvalue
----------
1|B
2|C
3|A
4|A
5|B
6|C
----------

Is it possible to perform a SELECT statement so that the output gets
ordered by column "myvalue" by B, then A, then C ?

Someting like:
SELECT * FROM mytable ORDER BY myvalue(''B'',''A'',''C'');

This is only an example of course but I wonder if this principle were
possible.

thanks,
Bart

解决方案

Bart Van der Donck wrote:

Hello,

I have a table, say

----------
ID|myvalue
----------
1|B
2|C
3|A
4|A
5|B
6|C
----------

Is it possible to perform a SELECT statement so that the output gets
ordered by column "myvalue" by B, then A, then C ?

Someting like:
SELECT * FROM mytable ORDER BY myvalue(''B'',''A'',''C'');



One way is to create another table, where you tell in what order values
should be:

table myvalue_order
----------------------
order_number | value
1 | B
2 | A
3 | C
----------------------

And then use a join, like this:

select mytable.* from mytable, value_order
where mytable.myvalue=value_order.value
order by myvalue_order.order_number;

Note that if a query like this is used, then rows where value with
order_number is used, are not displayed at all. If you want to get those
values also, you could use:

select mytable.* from mytable
left outer join value_order on mytable.myvalue=value_order.value
order by myvalue_order.order_number;

But this would place rows without order number at the first place. You
could of course use opposite order numbers

table myvalue_order
----------------------
order_number | value
3 | B
2 | A
1 | C
----------------------

And then do ... order by order_number desc;

And null values should be at the bottom of the list.


Bart Van der Donck wrote:

Hello,

I have a table, say

----------
ID|myvalue
----------
1|B
2|C
3|A
4|A
5|B
6|C
----------

Is it possible to perform a SELECT statement so that the output gets
ordered by column "myvalue" by B, then A, then C ?

Someting like:
SELECT * FROM mytable ORDER BY myvalue(''B'',''A'',''C'');



One way is to create another table, where you tell in what order values
should be:

table myvalue_order
----------------------
order_number | value
1 | B
2 | A
3 | C
----------------------

And then use a join, like this:

select mytable.* from mytable, value_order
where mytable.myvalue=value_order.value
order by myvalue_order.order_number;

Note that if a query like this is used, then rows where value with
order_number is used, are not displayed at all. If you want to get those
values also, you could use:

select mytable.* from mytable
left outer join value_order on mytable.myvalue=value_order.value
order by myvalue_order.order_number;

But this would place rows without order number at the first place. You
could of course use opposite order numbers

table myvalue_order
----------------------
order_number | value
3 | B
2 | A
1 | C
----------------------

And then do ... order by order_number desc;

And null values should be at the bottom of the list.


"Bart Van der Donck" <ba**@nijlen.com> wrote in message

ID|myvalue
----------
1|B
2|C
3|A
4|A
5|B
6|C
----------

Is it possible to perform a SELECT statement so that the output gets
ordered by column "myvalue" by B, then A, then C ?



Does

SELECT * FROM mytable ORDER BY if(myvalue=''A'', 2, ...);

work?


这篇关于操纵ORDER BY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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