mysql函数left()、length()使用

left(code, 8)表示取code字段从左截取8位;

length(code)=10表示code字段的长度是10位

select * from (
SELECT code as id ,name as text ,countycode as pid FROM dt_base_unitcode 
where length(code)=8 
UNION ALL
SELECT code as id ,name as text ,left(code, 8) as pid FROM dt_base_unitcode 
where length(code)=10
UNION ALL
SELECT code as id ,name as text ,citycode as pid FROM dt_base_countycode
UNION ALL
SELECT code as id ,name as text ,'' as pid FROM dt_base_citycode
) a
ORDER BY id+0 asc

1.mysql中length字符长度函数使用方法

https://blog.csdn.net/u010599560/article/details/72818944

2.MySQL字符串函数:字符串截取

http://justdo2008.iteye.com/blog/1141609

猜你喜欢

转载自blog.csdn.net/l_degege/article/details/82803465