Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL搜索查询使用降序限制1并使用左键_Mysql_Sql_Mysql Workbench - Fatal编程技术网

MYSQL搜索查询使用降序限制1并使用左键

MYSQL搜索查询使用降序限制1并使用左键,mysql,sql,mysql-workbench,Mysql,Sql,Mysql Workbench,我有一张这样的桌子 ID CODE NAME '1', '201600001', 'abc' '2', '201600002', 'bcd' '3', '201700003', 'def' 然后我想通过降序获得代码,从表中选择id、code、name\u示例顺序按代码desc limit 1;但是在选择代码时,我只需要前4个字符,因为代码是一个字符串。呃,这就是你想要的吗 select a.id, left(s.code, 4) as code, s.name from t

我有一张这样的桌子

ID    CODE        NAME
'1', '201600001', 'abc'
'2', '201600002', 'bcd'
'3', '201700003', 'def'

然后我想通过降序获得代码,从表中选择id、code、name\u示例顺序按代码desc limit 1;但是在选择代码时,我只需要前4个字符,因为代码是一个字符串。

呃,这就是你想要的吗

select a.id, left(s.code, 4) as code, s.name
from table_sample s
order by s.code desc
limit 1;

这是你想要的吗

select a.id, left(s.code, 4) as code, s.name
from table_sample s
order by s.code desc
limit 1;

这是我的尝试

SELECT ID, SUBSTRING(CODE, 0, 4), NAME FROM tbl_sample ORDER BY CODE DESC LIMIT 1 

这有什么好处吗?

这是我的尝试

SELECT ID, SUBSTRING(CODE, 0, 4), NAME FROM tbl_sample ORDER BY CODE DESC LIMIT 1 
有什么好处吗