TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-24-2008, 04:08 PM   #1 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default LEFT JOIN'ing 2 tables a particular way

Sa if I have two mysql tables and the 1st one is the 'topic' table and the other is a 'reply', there are many replies and one topic for each set of replies, How could I LEFT JOIN the last reply to te topics query (when selecting the topics data from the database), without having 2 queries, is this possible, if so how would I do this.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 01-24-2008, 04:13 PM   #2 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Just to clarify, you'll be selecting a topic from the topic table and you want to select the most recent reply to that topic from the replies table?

Alan
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Old 01-24-2008, 04:18 PM   #3 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Yep, most recent reply to that topic. All in 1 query, So topic details and the most recent reply to that topic' details will be the result.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 01-24-2008, 07:30 PM   #4 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Code:
SELECT r.*, t.* FROM replies r LEFT JOIN topics t ON(r.topic_id=t.id) ORDER BY r.reply_added DESC LIMIT 0,1
...?
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-24-2008, 08:25 PM   #5 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Ah, thanks I'll give that a try :).
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 01-25-2008, 01:05 PM   #6 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Okay after realizing that this wasn't what I wanted when I went home, I thought I'd mention this is more as of a blog type script. My sql is set up like this atm:


Code:
$db->set_query("SELECT t.*, u.user_id,u.user_name FROM `{$config['db_prefix']}topics` t, `{$config['db_prefix']}users` u WHERE t.topic_parent = '{$id}' AND u.user_name = t.topic_author ORDER BY `topic_last_posted` DESC");
Now I just need to get the latest reply to this topic and everything will be well, i tried different approaches like GROUP BY didn't work and now I'm back here.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 01-25-2008, 06:34 PM   #7 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

And why didn't my solution help you? That's what it does: it grabs info about the last reply and the topic associated with it.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 01-25-2008, 07:22 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

It sounds more like he wants to query for the latest topics and bring back the latest reply to each of those topics in a joined result as opposed to querying for the latest reply and joining the topic -- a subtle but important difference.

I'm not entirely sure that what is being asked for can be done with a single query (prove me wrong ).
Salathe is offline  
Reply With Quote
Old 01-25-2008, 08:17 PM   #9 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

actually i'm grabbing all topics in a forum and wanting to get the latests reply for that topic in 1 query.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 08:29 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design