|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
MySQL 5 - Select distinct
I have a table that is a bit like this
id col1 col2 col3 1 abc A 123hello 2 abc B 123hello 3 abc C 1234hello 4 efg A bla bla 5 efg B bla bla 6 hij A hello again 7 hij B hello again Now I would like to search for something say the word 'hello' As you can see there are 5 lines with the word hello in them. Then I would like to look at col1 and col 2 and select only the lines such that I get unique results from column1 with the highest values from col2. So in the above table 'hello' matches rows with id's 1,2,3 and 6 and 7. Now we look at col1 and find three entries with abc. then we look at col2 and see A,B,C and C is the highest so we return that row. Then we look and see rows 6, 7 are both 'hij' and then look at col2 and see A & B. B is higest so we return row 7. So the final output is 3 abc C 1234hello 7 hij B hello again Can anyone give me the sql query for this? Thanks |
|
#2
|
||||
|
||||
|
You have logic in your query so unless you want to write an obscene query (which may not even be possible) I would recommend you either do it with a stored procedure or in your application.
__________________
life is a game.... Have fun ----------------------------- http://www.phpwomen.org strength in unity PHPCommunity IRC #phpc on freenode |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > MySQL 5 - Select distinct |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|
|