Gayner Posted December 16, 2009 Share Posted December 16, 2009 Ok so i have no idea i only know how to run 1 query i can't use inner join or if i need to? But this i waht I want to do! SELECT * FROM users ORDER BY id DESC LIMIT 1 AND ALSO Select * from Prayers and Prayed so i can echo those out differently, so at the end of my result i can echo out this "We have a total of XX Prayers and People have XXX Prayed times. Our newest member is ____ How do i make it only 1 big query do u mind? thx Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/ Share on other sites More sharing options...
Deoctor Posted December 16, 2009 Share Posted December 16, 2009 Actually what is the question u are asking for?? u want from the newest member ____ Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978288 Share on other sites More sharing options...
Gayner Posted December 16, 2009 Author Share Posted December 16, 2009 Actually what is the question u are asking for?? u want from the newest member ____ yea and count * from prayers and prayed and beable to display them echo in php Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978290 Share on other sites More sharing options...
Gayner Posted December 16, 2009 Author Share Posted December 16, 2009 like kinda like this but iuno what im doing help me.. query = "SELECT COUNT(prayers),(prayed) FROM users ORDER BY id DESC LIMIT 1"; Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978293 Share on other sites More sharing options...
Deoctor Posted December 16, 2009 Share Posted December 16, 2009 if i am right u want the count of prayers and count of prayed for each player or any other thing? by the way if possible paste me the structure with sample data of the table.. Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978294 Share on other sites More sharing options...
Gayner Posted December 16, 2009 Author Share Posted December 16, 2009 if i am right u want the count of prayers and count of prayed for each player or any other thing? by the way if possible paste me the structure with sample data of the table.. i just want to count all prayers/prayed... but i can't do that cause i have to LIMIT it to 1 to get only the newest member... so like.. i only want to use 1 mysql query plz thx Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978296 Share on other sites More sharing options...
Deoctor Posted December 16, 2009 Share Posted December 16, 2009 SELECT COUNT(prayers),COUNT(prayed) FROM users WHERE id=1 Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978299 Share on other sites More sharing options...
Gayner Posted December 16, 2009 Author Share Posted December 16, 2009 SELECT COUNT(prayers),COUNT(prayed) FROM users WHERE id=1 id = 1? i got count to work but i need to show newest user too his field name is user_name but i can only do that if i LIMIT 1 but if I LIMIT 1 how do i get count prayed still working or does that ignore the function ? Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978301 Share on other sites More sharing options...
ldb358 Posted December 16, 2009 Share Posted December 16, 2009 i would just loop through $results = mysql_query("SELECT * FROM users ORDER BY id DESC"); $i=0; $first= ''; $prayers = 0; $prayed = 0; while($row= mysql_fetch_array($results)){ if($i==0){ $first = $row['user_name']; $i++; } $prayers += $row['prayers']; $prayed += $row['prayed']; } echo "We have a total of $prayers Prayers and People have $prayed Prayed times. Our newest member is $first"; Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978303 Share on other sites More sharing options...
Deoctor Posted December 16, 2009 Share Posted December 16, 2009 limit 1 is the function to use to the multiple results to be reduced to one. here u are using the count option obviously it will be one. if u want the count for the newest member then u need to specify the max of the date of his joining or else u need to check his last updation Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978304 Share on other sites More sharing options...
Gayner Posted December 16, 2009 Author Share Posted December 16, 2009 $results = mysql_query("SELECT * FROM users ORDER BY id DESC"); $i=0; $first= ''; $prayers = 0; $prayed = 0; while($row= mysql_fetch_array($results)){ if($i==0){ $first = $row['user_name']; $i++; } $prayers += $row['prayers']; $prayed += $row['prayed']; } echo "We have a total of $prayers Prayers and People have $prayed Prayed times. Our newest member is $first"; Sir this works nice, but only 1 problem, i can't be calling my whole user table each time a refresh happens, (my 1.99$/month server will crash in 2seoncds) is there anyway to LIMIT 1 to that query? Or is my proposal just impossible w/o 2 seperate queries? lol thanks for ur help man.. appreciate it alot broski. Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978308 Share on other sites More sharing options...
btherl Posted December 16, 2009 Share Posted December 16, 2009 So you want 3 data items: 1. The most recent user's name 2. The sum of the prayers column for all users 3. The sum of the prayed column for all users Is that right? I'm a bit confused as to whether you want the sum or the count. In any case, you should do it with two queries. SELECT user_name FROM users ORDER BY id DESC LIMIT 1 SELECT SUM(prayed) as sum_prayed, SUM(prayers) as sum_prayers FROM users I don't see any benefit to using one query only in this situation. Mysql can probably do the first query quickly using an index. If you find you have a performance problem with scanning the whole users table to find these counts, you can store the sums in a seperate table and update them every time prayers or prayed is updated for a user. Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978311 Share on other sites More sharing options...
Gayner Posted December 16, 2009 Author Share Posted December 16, 2009 my friend jsut helped me.. hella i love him no homo here's SOLVED: $query = "SELECT sum(prayers), sum(prayed), (select user_name from users order by id desc limit 1) as `latest` from users"; $result = mysql_query($query); $row = mysql_fetch_array($result); print_r($row); $newest_member = $row['user_name']; :-) enjoy thx !! Link to comment https://forums.phpfreaks.com/topic/185316-help-me-on-this-mysql-query-i-only-want-1-query/#findComment-978313 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.