我有一张桌子,我想用orderby子字符串进行选择,但这不起作用

时间:2018-09-25 13:15:56

标签: mysql select substring

我使用的是MySQL,而不是SQL 我有这张桌子,我想按没有“ M”的顺序选择那个桌子 那么该怎么做呢? 这是我创建的表

CREATE TABLE test (
  id VARCHAR(10)
);  
INSERT INTO test (id) VALUES ('M1');  
INSERT INTO test (id) VALUES ('M2');  
INSERT INTO test (id) VALUES ('M3');  
INSERT INTO test (id) VALUES ('M4');  
INSERT INTO test (id) VALUES ('M5');  
INSERT INTO test (id) VALUES ('M6');  
INSERT INTO test (id) VALUES ('M7');  
INSERT INTO test (id) VALUES ('M8');  
INSERT INTO test (id) VALUES ('M9');  
INSERT INTO test (id) VALUES ('M10');  
INSERT INTO test (id) VALUES ('M11');  
INSERT INTO test (id) VALUES ('M12');  
INSERT INTO test (id) VALUES ('M13');  
INSERT INTO test (id) VALUES ('M14');  
INSERT INTO test (id) VALUES ('M15');  
INSERT INTO test (id) VALUES ('M16');  
INSERT INTO test (id) VALUES ('M17');  
INSERT INTO test (id) VALUES ('M18');  
INSERT INTO test (id) VALUES ('M19');  
INSERT INTO test (id) VALUES ('M20');  
INSERT INTO test (id) VALUES ('M21');  
INSERT INTO test (id) VALUES ('M22');  
INSERT INTO test (id) VALUES ('M23');  
INSERT INTO test (id) VALUES ('M24');  
INSERT INTO test (id) VALUES ('M25');  
INSERT INTO test (id) VALUES ('M26');  
INSERT INTO test (id) VALUES ('M27');  
INSERT INTO test (id) VALUES ('M28');  
INSERT INTO test (id) VALUES ('M29');  
INSERT INTO test (id) VALUES ('M30');  
INSERT INTO test (id) VALUES ('M31');  
INSERT INTO test (id) VALUES ('M32');  
INSERT INTO test (id) VALUES ('M33');  
INSERT INTO test (id) VALUES ('M34');  
INSERT INTO test (id) VALUES ('M35');  
INSERT INTO test (id) VALUES ('M36');  
INSERT INTO test (id) VALUES ('M37');  
INSERT INTO test (id) VALUES ('M38');  
INSERT INTO test (id) VALUES ('M39');  
INSERT INTO test (id) VALUES ('M40');  
INSERT INTO test (id) VALUES ('M41');  
INSERT INTO test (id) VALUES ('M42');  
INSERT INTO test (id) VALUES ('M43');  
INSERT INTO test (id) VALUES ('M44');  
INSERT INTO test (id) VALUES ('M45');  
INSERT INTO test (id) VALUES ('M46');  
INSERT INTO test (id) VALUES ('M47');  
INSERT INTO test (id) VALUES ('M48');  
INSERT INTO test (id) VALUES ('M49');  
INSERT INTO test (id) VALUES ('M50');  
INSERT INTO test (id) VALUES ('M51');  
INSERT INTO test (id) VALUES ('M52');  
INSERT INTO test (id) VALUES ('M53');  
INSERT INTO test (id) VALUES ('M54');  
INSERT INTO test (id) VALUES ('M55');  
INSERT INTO test (id) VALUES ('M56');  
INSERT INTO test (id) VALUES ('M57');  
INSERT INTO test (id) VALUES ('M58');  
INSERT INTO test (id) VALUES ('M59');  
INSERT INTO test (id) VALUES ('M60');  
INSERT INTO test (id) VALUES ('M61');  
INSERT INTO test (id) VALUES ('M62');  
INSERT INTO test (id) VALUES ('M63');  
INSERT INTO test (id) VALUES ('M64');  
INSERT INTO test (id) VALUES ('M65');  
INSERT INTO test (id) VALUES ('M66');  
INSERT INTO test (id) VALUES ('M67');  
INSERT INTO test (id) VALUES ('M68');  
INSERT INTO test (id) VALUES ('M69');  
INSERT INTO test (id) VALUES ('M70');  
INSERT INTO test (id) VALUES ('M71');  
INSERT INTO test (id) VALUES ('M72');  
INSERT INTO test (id) VALUES ('M73');  
INSERT INTO test (id) VALUES ('M74');  
INSERT INTO test (id) VALUES ('M75');  
INSERT INTO test (id) VALUES ('M76');  
INSERT INTO test (id) VALUES ('M77');  
INSERT INTO test (id) VALUES ('M78');  
INSERT INTO test (id) VALUES ('M79');  
INSERT INTO test (id) VALUES ('M80');  
INSERT INTO test (id) VALUES ('M81');  
INSERT INTO test (id) VALUES ('M82');  
INSERT INTO test (id) VALUES ('M83');  
INSERT INTO test (id) VALUES ('M84');  
INSERT INTO test (id) VALUES ('M85');  
INSERT INTO test (id) VALUES ('M86');  
INSERT INTO test (id) VALUES ('M87');  
INSERT INTO test (id) VALUES ('M88');  
INSERT INTO test (id) VALUES ('M89');  
INSERT INTO test (id) VALUES ('M90');  
INSERT INTO test (id) VALUES ('M91');  
INSERT INTO test (id) VALUES ('M92');  
INSERT INTO test (id) VALUES ('M93');  
INSERT INTO test (id) VALUES ('M94');  
INSERT INTO test (id) VALUES ('M95');  
INSERT INTO test (id) VALUES ('M96');  
INSERT INTO test (id) VALUES ('M97');  
INSERT INTO test (id) VALUES ('M98');  
INSERT INTO test (id) VALUES ('M99');  
INSERT INTO test (id) VALUES ('M100');  
INSERT INTO test (id) VALUES ('M101');  
INSERT INTO test (id) VALUES ('M102');  
INSERT INTO test (id) VALUES ('M103');  
INSERT INTO test (id) VALUES ('M104');  
INSERT INTO test (id) VALUES ('M105');  
INSERT INTO test (id) VALUES ('M106');  
INSERT INTO test (id) VALUES ('M107');  
INSERT INTO test (id) VALUES ('M108');  
INSERT INTO test (id) VALUES ('M109');  
INSERT INTO test (id) VALUES ('M100');  
INSERT INTO test (id) VALUES ('M111');  
INSERT INTO test (id) VALUES ('M102');  
INSERT INTO test (id) VALUES ('M103');  
INSERT INTO test (id) VALUES ('M104');  
INSERT INTO test (id) VALUES ('M105');  
INSERT INTO test (id) VALUES ('M106');  
INSERT INTO test (id) VALUES ('M107');  
INSERT INTO test (id) VALUES ('M108');  
INSERT INTO test (id) VALUES ('M109');  
INSERT INTO test (id) VALUES ('M110');  
INSERT INTO test (id) VALUES ('M111');  
INSERT INTO test (id) VALUES ('M112');  
INSERT INTO test (id) VALUES ('M113');  
INSERT INTO test (id) VALUES ('M114');  
INSERT INTO test (id) VALUES ('M115');  
INSERT INTO test (id) VALUES ('M116');  
INSERT INTO test (id) VALUES ('M117');  
INSERT INTO test (id) VALUES ('M118');  
INSERT INTO test (id) VALUES ('M119');  
INSERT INTO test (id) VALUES ('M120');  
INSERT INTO test (id) VALUES ('M121');  
INSERT INTO test (id) VALUES ('M122');  
INSERT INTO test (id) VALUES ('M123');  
INSERT INTO test (id) VALUES ('M124');  
INSERT INTO test (id) VALUES ('M125');  
INSERT INTO test (id) VALUES ('M126');  
INSERT INTO test (id) VALUES ('M127');  
INSERT INTO test (id) VALUES ('M128');  
INSERT INTO test (id) VALUES ('M129'); 

我希望代码选择像这样:
从测试顺序中按Substring(id,1)选择*;
我想不带“ M”的订单,所以订单是从1-> 129
知道怎么做吗?
请测试我测试失败的原因
顺便说一句,有人可以引导我如何使用小提琴数据库来举例说明吗?我不知道该怎么办,所以我的问题太长了^^!

2 个答案:

答案 0 :(得分:1)

尝试一下:

Select id from test order by cast(Substring(id,1) as signed);

答案 1 :(得分:1)

您可以使用此:

SELECT *, TRIM(LEADING "M" FROM id) as newid FROM test ORDER BY cast(newid as unsigned);