开发者

INNER JOIN and SELECT of field with less string length

开发者 https://www.devze.com 2023-04-05 22:45 出处:网络
Because of a bad structure of a MySQL table (which I don\'t have the control to change) I figured that the best solution for what I am trying to achieve is to use the result with less string length. T

Because of a bad structure of a MySQL table (which I don't have the control to change) I figured that the best solution for what I am trying to achieve is to use the result with less string length. This is my current query:

SELECT cpc
FROM cpc_options
INNER JOIN cpc_targets开发者_如何学运维
WHERE targeted_name like '%country%' AND target_id = cpc_id

Basically there is a table with countries and another one with ids and their cpc, so joining both I can get the cpc for a country. However, for example US appears many times in the first table, with different names: United States, United States and Others, etc...

I figured that I need to select the targeted_name that has the small string length count... How can I add that to the query above?


Order by the length of targeted_name and limit it to 1 result:

SELECT cpc
FROM cpc_options
INNER JOIN cpc_targets
WHERE targeted_name like '%country%' AND target_id = cpc_id 
ORDER by LENGTH(targeted_name) asc limit 1;
0

精彩评论

暂无评论...
验证码 换一张
取 消