Jump to content

[SOLVED] What is wrong with this query?!?!


HaLo2FrEeEk

Recommended Posts

I cannot figure it out, it seems perfect to me, but I'm tired right now.  Its supposed to get the latest topic_id from ONLY forum 1, which is news, where the title of the post starts with "Weekly Update: ", but it is returning the latest value from the entire database whose title starts with "Weekly Update: ", what am I doing wrong:

 

SELECT post_text
FROM phpbb_posts_text
WHERE post_id = (
  SELECT topic_first_post_id
  FROM phpbb_topics
  WHERE topic_id = (
    SELECT max(t.topic_id)
    FROM phpbb_forums f,
         phpbb_topics t
    WHERE f.forum_id = '1'
    AND t.topic_title like 'Weekly Update: %'
    )
  )

Link to comment
Share on other sites

That's because you haven't added a join condition, like

 

AND f.id = t.id

 

You'll need to use the appropriate column which links f and t.

 

If you're still having trouble, can you post the table definitions?

Link to comment
Share on other sites

There is no f.id, there is f.forum_id, and there is t.topic_id, and they are not equal, I want results from forum_id 1, which is news, and it'll be returning a topic id which will be int he 4 and 5 hundreds.  It is a default phpbb installation, if you have access to one to look at.  What do you mean by table definitions?

Link to comment
Share on other sites

Guest footballkid4

The subquery should look something like:

SELECT max(t.topic_id) 
FROM phpbb_forums f, phpbb_topics t
WHERE t.forum_id=f.forum_id 
AND f.forum_id =1
AND t.topic_title LIKE 'Weekly Update: %'

Link to comment
Share on other sites

Guest footballkid4

Basically the t.forum_id=f.forum_id tells SQL to match up the columns in that manner.

 

Without matching those up, you never know how your data will match up.  You could have a topic from forum 3 that will show up with data from forum 2...since you never told MySQL to match it up.  Basically, MySQL reads that statement and thinks:

 

Take all of the rows from the specified table with the specified WHERE clause, and match them up so that the topic table's forum_id is the same as the forum table's forum_id.  This will ensure that all topics in forum 3 are associated with forum 3 data.  It's relatively hard to explain, which is why I'm going to end it here.  All that matters is that it's a way of telling MySQL where to sync up the data.

Link to comment
Share on other sites

I understand completely, that was a good explanation (I have a pretty good head on my shoulders, just sometimes I miss things, and I just got home from work, so I'm kinda tired, and missed something  :-\)

 

Anywho, I didn't see that phpbb_topics had a forum_id row in it, otherwise I might have figured it out...maybe...anyways...

 

Thank you very much for your help, it is much appreciated.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.