Solved

Select bigger string from 2 or more strings in a column

Posted on 2009-06-27
8
374 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
8 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 63 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 31

Assisted Solution

by:awking00
awking00 earned 62 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now