mysql按字段顺序排序不符合我的期望

时间:2019-04-08 20:01:24

标签: mysql

我想使用FIELD参数对我的SQL输出进行排序,但是当我这样做时,它首先吐出我不需要的结果,然后它吐出我想要的结果。在结果之上,它首先吐出。如果那有道理;)

如何先吐出定义的值,然后吐出其余值?

SQL查询:

SELECT * FROM `product_specs` WHERE itemid = '$id' ORDER BY FIELD(spec_type, 'value 1', 'value 2');

当前输出:

Value 5 
Value 6

Value 1
Value 2

想要的输出:

Value 1
Value 2 

Value 5
Value 6

我知道case参数,但是我不想使用它,因为它不那么容易更新。

谢谢

1 个答案:

答案 0 :(得分:2)

public interface IBasiestEventInterface { string P1 { get; } } public interface IBaserEventInterface : IBasiestEventInterface { string P2 { get; } } public interface IBaseEventInterface : IBaserEventInterface { string P3 { get; } } public class TheEvent : IBaseEventInterface { public string P1 { get; } = "A"; public string P2 { get; } = "B"; public string P3 { get; } = "C"; } [TestFixture] public class MassTransitTests { [Test] public async Task CanBeConsumedAsInterfaceType() { var semaphore = new SemaphoreSlim(0); var publisher = Bus.Factory.CreateUsingAzureServiceBus(c => { c.Host(MassTransitTestsHelper.BusConnectionString, h => { }); }); var consumer1 = Bus.Factory.CreateUsingAzureServiceBus(c => { c.Host(MassTransitTestsHelper.BusConnectionString, h => { }); c.ReceiveEndpoint("test_receive_endpoint", e => { e.Handler((MessageHandler<IBaseEventInterface>) (_ => { semaphore.Release(); return Task.CompletedTask; })); }); }); await publisher.StartAsync(); await consumer1.StartAsync(); await publisher.Publish<IBaseEventInterface>(new TheEvent()); (await semaphore.WaitAsync(10.Seconds())).Should().BeTrue(); } } 子句中使用CASE,将ORDER BY(未找到)转换为0(在所有值之后),如下所示:

999