MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 20th, 2012, 08:35 PM
lottos lottos is offline
competitions at lottos.com.au
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 251 lottos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 19 h 6 m 7 sec
Reputation Power: 10
Select all rows and select just first row?

Currently do a select to produce rows of posts for a specific topic:

$queryposts = $db->query("SELECT p.pid, p.author, p.message, p.subject, p.dateline, m.username FROM posts p INNER JOIN members m ON m.username = p.author WHERE.... ORDER BY p.pid ASC");



I would like to ALSO select the FIRST post row for the same topic, assign it to a variable. I could do this via:


$firstpostquery = $db->query("SELECT pid FROM posts WHERE... ORDER BY dateline LIMIT 1");
$isthefirstpost = $db->fetch_array($firstpostquery);
$db->free_result($firstpostquery);


However the second select seems inefficient because I'm already grabbing all rows in the first select and the second select has to go through the entire posts table again.


Is there a way to use the first select and also do a sub select for also obtaining the first row - or some other method that is more efficient?
__________________
Best website for competitions online to win anything and everything.

Reply With Quote
  #2  
Old December 20th, 2012, 09:11 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,380 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 54 m 8 sec
Reputation Power: 4140
since you're retrieving all the rows that meet your criteria, the function to extract the first row's data should be performed in your application language

that's not only the easiest but also the most efficient
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old December 21st, 2012, 05:50 AM
lottos lottos is offline
competitions at lottos.com.au
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 251 lottos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 19 h 6 m 7 sec
Reputation Power: 10
Quote:
Originally Posted by r937
since you're retrieving all the rows that meet your criteria, the function to extract the first row's data should be performed in your application language

that's not only the easiest but also the most efficient



Thanks Rudy. Great advice. In case anyone else is looking for a similar solution, a good example can be found here:
http://stackoverflow.com/questions/4742659/php-loop-through-query-first-row

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Select all rows and select just first row?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap