how to match with like in other table and count

回眸只為那壹抹淺笑 提交于 2019-12-25 18:34:44

问题


i have 2 tables areas2013

row_id | area              |teamleader
1      |  1234-Asia        |   Joe 
2      |  12345-Europe     |   Juan  
3      | 123456-UK         |   Ple 

and f12

row_id| eacode
1     | 1234
2     | 12345
3     | 1234

as you can see i want to match the eacode and area using like% and count how many eacode.

i want something like this in php

eacode| area | count | teamleader 
1234  | Asia  |   2    |  Joe
12345 | Europe|   1    |  Juan

sorry for my bad english


回答1:


Barmar's answer is good I just tweaked it a bit to get the exact output you requested:

SELECT eacode, SUBSTRING_INDEX(SUBSTRING_INDEX(area, '-', 2), '-', -1) as area, count(*) `count`, teamleader
FROM f12
JOIN areas2013 ON area like CONCAT(eacode, '-%')
GROUP BY eacode



回答2:


SELECT eacode, substr(area, locate('-', area) + 1) area, count(*) `count`, teamleader
FROM f12
JOIN areas2013 ON area like CONCAT(eacode, '-%')
GROUP BY eacode


来源:https://stackoverflow.com/questions/17344504/how-to-match-with-like-in-other-table-and-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!