Page 1 of 1
Problem in Sorting two resultset..
Posted: Mon Oct 08, 2007 2:51 am
by ashishhbti
I ave a problem in sorting two resultset..
Supoose we have a follwing field in database..
id name vote type
1 ashish 2 1
2 ashish 4 2
i want to select top 5 records(according to vote) of type 1 and top voted 5 records of type 2 ....
then want to show on page these 10 records on sorting order according to vote ..
any suggestion ??
Please help me...
Thanks
Ashish...
2 Queries
Posted: Mon Oct 08, 2007 5:34 am
by N1gel
Prehaps do it with 2 different queries and the join the results using php.
Code: Select all
$result1 = mysql_query('select name, votes from table where type = 1 order by votes desc limit 5');
$result2 = mysql_query('select name, votes from table where type = 2 order by votes desc limit 5');
$row1 = mysql_fetch_row($result1);
$row2 = mysql_fetch_row($result2);
$index1=0;
$index2=0;
while($index1 != 4 and $index2 != 4)
{
if($row1[1] > $row2[1])
{
//Print $row1[0] the name
$row1 = mysql_fetch_row($result1);
$index1++;
}
else
{
//Print $row2[0] the name
$row2 = mysql_fetch_row($result2);
$index2++;
}
}
This is just an example and i haven't tested any of the code but hopefully it gives you an idea for the solution

Posted: Mon Oct 08, 2007 7:36 pm
by feyd
I would probably use a single query.. a UNION.
Posted: Tue Oct 09, 2007 4:41 am
by ashishhbti
Problem Solved..

Thanks N1gel.
@feyd..
it would better if you can provied single query.
Thanks to all.
Ashish.
Posted: Tue Oct 09, 2007 9:45 am
by feyd
ashishhbti wrote:@fyed..
It's
feyd. Common mistake.
ashishhbti wrote: it would better if you can provied single query.
This is a teaching site. I will not spoon feed you an answer.
Posted: Wed Oct 10, 2007 1:04 am
by ashishhbti
feyd wrote:It's feyd..
now is it ok ??:)
happy ...
anything [s]4[/s]
for [s]u[/s]
you sir

.
Problem solved ...so [s]u[/s]
you no need to worry ..
i just said it would be better ...
[s]btw[/s]
by the way Thanks..
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:11. Please use proper, complete spelling when posting in the forums. AOL Speak, leet speak and other abbreviated wording can confuse those that are trying to help you (or those that you are trying to help). Please keep in mind that there are many people from many countries that use our forums to read, post and learn. They do not always speak English as well as some of us, nor do they know these aberrant abbreviations. Therefore, use as few abbreviations as possible, especially when using such simple words.
Some examples of what not to do are ne1, any1 (anyone); u (you); ur (your or you're); 2 (to too); prolly (probably); afaik (as far as I know); etc.