将sql中的邮政编码中的值拆分为组和结果

时间:2017-04-27 12:03:11

标签: php sql split substring

我在查询的这一部分遇到了一些问题,希望有人可以建议我,目前我将邮政编码分组为完整的邮政编码,但我想将此值拆分,并按照邮政编码的第一部分进行分组

也许我忽视了对此的简单解决方案?

这是我当前的查询

select *, count(jobline_pickup_add_postalcode) as totals
FROM rhrj_jobline
WHERE jobline_date >= '$start' AND jobline_date <= '$end'
GROUP BY jobline_pickup_add_postalcode

任何建议都很棒

编辑:

我的邮政编码看起来像这样

KA12 0RA - 1 
KA15 1JG - 26 
KA15 2AT - 1 
KA15 2LF - 1 
KA151JG/PA2 6LA - 2 

1 个答案:

答案 0 :(得分:0)

选择SUBSTRING_INDEX(jobline_pickup_add_postalcode,'',1)AS papc,count(jobline_pickup_add_postalcode)作为总计FROM rhrj_jobline WHERE jobline_date&gt; ='$ start'AND jobline_date&lt; ='$ end'GROUP BY papc