0

我有一个包含 4 个 varchar 字段的表,我需要根据列是否为空来排序。

所以我的表结构是:

start_price     reserve_price     buy_now_price    current_bid_amount
---------------------------------------------------------------------
50.00           50.00             empty (not null) (Null)
190             190               empty (not null) (Null)
150             150               empty (not null) (Null)
20              150               empty (not null) (Null)
550             600               empty (not null) (Null)

我在下面有一个订单条款,但似乎没有正确订购。目前,结果按 190、150、50、20、550 排序。

ORDER BY COALESCE(CAST( al. current_bid_amountAS SIGNED), CAST( al. buy_now_priceAS SIGNED), CAST( al. start_priceAS SIGNED), CAST( al. reserve_priceAS SIGNED)) ASC

基本上我需要根据 current_bid_amount、buy_now_price、start_price、reserve_price 按最低价排序。因此,如果 current_bid_amount AND buy_now_price 为空,则使用 start_price。如果 current_bid_amount 为空但 buy_now_price 不是,则使用 buy_now_price。如果 current_bid_amount 中有一个值,请使用该值进行订购。

非常感谢。

4

1 回答 1

1

好吧,将空字符串转换为有符号的是 0。因此,您的 buy_now_price 可能会覆盖所有内容,因为它为空但不为空。然后,您基本上不会进行排序,因为您的合并将始终对您的所有项目评估为 0。

mysql> select cast('' as signed);
+--------------------+
| cast('' as signed) |
+--------------------+
|                  0 |
+--------------------+
1 row in set, 1 warning (0.00 sec)

mysql> select cast(null as signed);
+----------------------+
| cast(null as signed) |
+----------------------+
|                 NULL |
+----------------------+
1 row in set (0.00 sec)
于 2013-04-09T22:45:19.787 回答