INNER JOIN and SELECT of field with less string length
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;
精彩评论