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
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 09-18-2008, 05:26 PM   #1 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 218
Thanks: 16
RobertK is on a distinguished road
Terminal [SQLITE2] Complicated SELECT with 2 sub-queries

Hi everyone, it's been a long time but I'm getting back into the swing of things. Started my own company, so it has been hectic of late.

I'm struggling with a complicated sub-query that traces through 3 tables to get the last poster in a forum. Everything else works absolutely great. Please be patient with me, since my brain is rather scrambled with this cryptic logic.

Here's the table structure:
sql Code:
CREATE TABLE users (
  uid INTEGER PRIMARY KEY,
  username VARCHAR(255),
  password VARCHAR(40),
  email VARCHAR(255),
  signature TEXT,
  timeoffset VARCHAR,
  dst BOOLEAN,
  datefmt VARCHAR(128),
  role INTEGER,
  spam INTEGER,
  since INTEGER,
  diskspace INTEGER
)

CREATE TABLE threads (
  tid INTEGER PRIMARY KEY,
  uid INTEGER,
  fid INTEGER,
  threadname VARCHAR(255),
  posts INTEGER,
  flags INTEGER
)

CREATE TABLE posts (
  pid INTEGER PRIMARY KEY,
  tid INTEGER,
  uid INTEGER,
  posted INTEGER,
  ip VARCHAR(30),
  content TEXT
)

Okay, got that? Here's the query. I'll even mark the second subquery, because I've no clue how to write it now.
sql Code:
SELECT fid, title, description,
  (
    SELECT COUNT(*) FROM threads
    WHERE threads.fid = fid
  ) AS "count",
  ( -- The trouble is right here...
    SELECT users.username AS "name",
    users.uid AS "uid"
    FROM threads JOIN users ON uid
    WHERE -- how do I limit this by the
          -- last post within the thread?
  )
  AS "lastposter" FROM forums ON fid

What I'm TRYING to do is this: get the ID/NAME of the last poster from the database. Currently, the software is only looking at the "forum list" view, so I don't want to run 18 queries on this. So the query gets all the forums, and then subqueries for the thread count, and then the last poster. Again, the last is my trouble.

So I'm kind of stuck. How should I rewrite this query? I organized my DB this way for simplicity, and because I'm still only novice/intermediate with MySQL/SQLite.

And just for the record, I know that just about the last thing the world needs is yet another forum package, but I'm writing this to learn a few key concepts (that aren't SQL).
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 09-19-2008, 09:29 PM   #2 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 218
Thanks: 16
RobertK is on a distinguished road
Default

Okay, I understand someone with inadequate knowledge of sub-selects leaving this topic untouched, but what about everyone else? SQLite supports 98% of MySQL's syntax.

I did manage to query within a forum for threads and their most recent poster. However, I'm still uncertain how to do this from the index without a per-forum query.

Here's the forum-specific query. However, I suspect that it'll return a result for each post in the thread.

sql Code:
SELECT DISTINCT(posts.tid) AS 'id',
threads.threadname AS 'name',
posts.posted AS 'posted',
posts.uid AS 'uid',
threads.posts AS 'posts',
(SELECT username FROM users WHERE users.uid = uid) AS 'who',
threads.flags AS 'flags' FROM posts
JOIN threads ON posts.tid = threads.tid
WHERE threads.fid = {$fid}
ORDER BY posts.posted ASC
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Old 09-24-2008, 04:24 PM   #3 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 469
Thanks: 26
sketchMedia is on a distinguished road
Default

EDIT:::: nvm i misread your second post. ehhhh long day.
__________________
sketchMedia is offline  
Reply With Quote
Old 09-24-2008, 05:42 PM   #4 (permalink)
The Addict
Top Contributor Good Samaritan 
 
Join Date: Jan 2008
Location: USA
Posts: 218
Thanks: 16
RobertK is on a distinguished road
Default

It happens.

Have any ideas about the first? I've got the second query working great now, but I haven't actually made progress on the second one.
__________________
Programmers are in a race with the Universe to create bigger and better idiot-proof programs, while the Universe is trying to create bigger and better idiots. So far the Universe is winning. - Rich Cook
RobertK is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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 06:45 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0