?
Solved

Select bigger string from 2 or more strings in a column

Posted on 2009-06-27
8
Medium Priority
?
393 Views
Last Modified: 2012-05-07
I want to make a group seletc that retrieves a distinct string from col1 and the larger string from col2.....
ex.
[col1]           [col2 ]  [col3]
1111          aaa        3
2222          bbb         3
1111          ccccc     5
2222          ddddd     5
3333          eeee       4
3333          f              1

results wanted!!
[col1]   [col2]  
1111    ccccc
2222    ddddd  
3333    eeee

col1 and col3 are string , col3 int with the length of the string in col2.....
i made several mysql query and sometimes retrieves the larger string, others not.

i know how to do this with php but i want to doit in a mysql query, maybe i need a sub query...
Thanks.
0
Comment
Question by:dmonzon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 252 total points
ID: 24729128
what version of mysql?

let's try this:
select t.*
  from yourtable t
 where len(col2) = ( SELECT max(len(i.col2)) from yourtable i where i.col1 = t.col1 )

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24729129
SELECT Col1, MAX(Col2)Col2
FROM urTable
GROUP BY Col1
0
 
LVL 41

Expert Comment

by:ralmada
ID: 24729183
I think you're looking for this:
select a.col1, a.col2
from yourtable a
join (select col1, max(col3) as mcol3 from yourtable group by col1) b on a.col1 = b.col1 and a.col3 = b.mcol3

Open in new window

0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
LVL 32

Assisted Solution

by:awking00
awking00 earned 248 total points
ID: 24736780
See attached.
select col1, col2
from yourtable y,
(select col1, max(len(col2)) col2_len
 from yourtable
 group by col1) x
where y.col1 = x.col1
  and len(y.col2) = x.col2_len;

Open in new window

0
 
LVL 1

Author Comment

by:dmonzon
ID: 24786500
sorry for the delay, i already resolve this the same day and i forgot to close the question, any way i will try your answers and let you know .....
thanks
0
 
LVL 41

Expert Comment

by:ralmada
ID: 25039499
Angel, Not sure why my query will not help the asker. Can you please advise?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question