Bestmatch Top-Row select

Hi there!

I am currently selecting the bestmatch in an old-fashioned kind of way:

select t1.x,t2.y,t2.z from t1, t2 where t1.x like concat(t2.y,'%') order by t2.y desc;

x is filled with telephonenumbers whereas y is populated with areacodes of different length, like 702,7025,70255,702555 and z holds the corresponding cityname for y.

Now my question is that I don't want to get ALL the possible matches with the bestmatch as first row, but I only want to get this one row as result. Currently my problem is that I always get each x-number as many times as there are y-entries and that is definitely causing problems.

As always and as you have already expected: this is a rather urgent matter and that's why this is valued at 500 points :-)

Thanks for your help!

Best regards,

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I don't think you can do it with MySQL, you have to extrapolate your data and use some kind of scripting to display in the format you need.
tillmanz123Author Commented:

I give you a small example:

x-column - a-column
12125551212 - 2006-01-01
13104451255 - 2006-01-02
12122224545 - 2006-01-03

y-column - z-column
1 - USA
1212 - NY
1212555 - DirectoryAssistence

select t1.x, t1.a, t2.z
from t1, t2
where t1.x like concat(t2.y,'%')
order by t2.y desc;

I want my result to look like this:


Maybe I am just too blind to see, but please tell me how to do that (apart from using my application to parse through the result set).

Best regards,

it would help to know which version of mysql that you are using.

my first suggestion would be this:

CREATE table `hold_it`
select t1.x, t1.a, t2.y, t2.z
from t1, t2
where t1.x like concat(t2.y,'%')
order by t2.y desc;

SELECT x, a, z FROM `hold_it` GROUP BY x

though the results of that query will technically be arbitrary


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

try this
select x,y,z from (select t1.x as x,t2.y as y,t2.z as z from t1, t2 where t1.x like concat(t2.y,'%') order by t2.y desc) as d group by x;
which is nothing more than what i suggestd rewritten as a subselect...
my code will work in 3.23+, his code will only work in 4.1+ and is substantially identical... which is especially funny since his initial comment is "I don't think mysql can do this"
virmajor, i agree with you, it is same as yours except with the sub query.
Also about about my first comment i wasn't clear about the tillman's result, until he explained in his later post.
fair enough
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.