954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

LIMIT Results from query !HELP

I can't seem to limit the result to a desired number. I have this code:

//GET THE TOTAL OF LEVEL 2
$result = mysql_query("SELECT COUNT(*) FROM agents WHERE (sponsor = '1' or sponsor =  '2' or sponsor = '3') LIMIT 0,1")or die(mysql_error());  
while($row = mysql_fetch_array( $result )) {
$lvl2parent = $row['COUNT(*)'];
echo "<b>Total Level 2 </b> : ";
echo "$lvl2parent ";


On the code above, the sponsors 1, 2, 3 has 2 records each. And as I see it, the LIMIT 0,1 applies to each instead of giving me ONLY 1 RESULT from the overall. I thought of using an array to echo all results and then choose 1 from that but I am not too familiar with it.

Hope you can help me. this is the final function to finish my project. Thanks!

codewalkz
Junior Poster in Training
51 posts since Nov 2009
Reputation Points: 10
Solved Threads: 2
 

The LIMIT always applies to the end result of the query. Even without the limit, the count(*) will only return 1 result row.

I'm not sure of what you try to achieve. If you want the count per sponsor, then you could try a union:

SELECT COUNT(*) FROM agents WHERE sponsor = '1'
UNION
SELECT COUNT(*) FROM agents WHERE sponsor = '2'
UNION
SELECT COUNT(*) FROM agents WHERE sponsor = '3'
pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Thanks for the reply. Anyway, I would like to retrieve all the downlines of the sponsors 1 - 3 but I need to choose a specific number of result from ALL those results NOT from each sponsor's result.

Example: 1 - 3 sponsors has 2 results in each so that would result to 6 members all in all. Now, I would like to get only 1 member from the 6 members.

I hope that would clear it. i am not so familiar. with this. thak you so much!

The LIMIT always applies to the end result of the query. Even without the limit, the count(*) will only return 1 result row.

I'm not sure of what you try to achieve. If you want the count per sponsor, then you could try a union:

SELECT COUNT(*) FROM agents WHERE sponsor = '1'
UNION
SELECT COUNT(*) FROM agents WHERE sponsor = '2'
UNION
SELECT COUNT(*) FROM agents WHERE sponsor = '3'
codewalkz
Junior Poster in Training
51 posts since Nov 2009
Reputation Points: 10
Solved Threads: 2
 

Then you could use this one:

SELECT * FROM agents WHERE (sponsor = '1' or sponsor =  '2' or sponsor = '3') LIMIT 1
pritaeas
Posting Expert
Moderator
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: