Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Numbering posts based on datetime in MySQL
Old 09-01-2011, 05:02 PM Numbering posts based on datetime in MySQL
Average Talker

Posts: 19
Trades: 0
I have the posts in my message board selected via a topic ID and ordered by their datetime. Eg:
Code:
SELECT * FROM forum WHERE topic_id = 9 ORDER BY datetime ASC
On another page I want to create a link that will point to any specific post in a topic. Eg:
Code:
http://www.example.com/forum/viewtopic.php?id=9#post-3
. . . which of course would be the third post in the topic. However, I'm having trouble getting that post number via a MySQL query. What I've tried is counting the number of posts that are older than the one that I'm trying to query. Eg:
Code:
SELECT forum.*, forum2.post_id FROM forum LEFT JOIN (SELECT COUNT(*) AS post_id FROM forum AS f2 WHERE f2.datetime <= forum.datetime) forum2 ON forum.id = forum2.id
. . . or something like that. The problem is that I of course can't compare the datetime in my LEFT JOIN to the datetime in my main SELECT, so I can't determine the number of posts that are older than the one I'm trying to query.

Is there any way to do a COUNT() outside of a LEFT JOIN but still give it a specific condition like in my WHERE clause? Something like:
Code:
SELECT *, COUNT(SELECT * FROM forum AS forum2 WHERE forum2.datetime <= forum.datetime) FROM forum
. . . which obviously doesn't work, but is there anything along those lines?

Thanks.
Kent O'Matic is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-01-2011, 05:48 PM Re: Numbering posts based on datetime in MySQL
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
You can't do that, a query returns a static block of rows comprising of the columns requested.

To add sequential numbering of the rows you will either have to write a stored procedure to create the recordset or add the numbering as you output the records to the user agent.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-01-2011, 06:22 PM Re: Numbering posts based on datetime in MySQL
Average Talker

Posts: 19
Trades: 0
I've written a query that generates a post number based on the MAX datetime, though.
Code:
SELECT post_id FROM forum 
LEFT JOIN (SELECT forum.topic_id, count(*) AS post_id FROM forum 
LEFT JOIN (SELECT topic_id, MAX(datetime) AS datetime FROM forum WHERE user_id = 'MYID' GROUP BY forum.topic_id) forum2 ON forum2.topic_id = forum.topic_id 
WHERE forum.datetime <= forum2.datetime GROUP BY forum.topic_id) f ON f.topic_id = forum.topic_id
. . . and this gives me the correct post ID for the most recent post in a topic by a particular user (whoever is defined by 'MYID'). It's inelegant but it works and allows me to compare two instances of the datetime field from a single table. I've been trying to tweak it so that the second LEFT JOIN gives me the datetime of the post that I'm querying rather than the MAX datetime, but I think that the problem is my ON clause.
Kent O'Matic is offline
Reply With Quote
View Public Profile
 
Old 09-01-2011, 06:33 PM Re: Numbering posts based on datetime in MySQL
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
it's also very inefficient.

I hope your system doesn't have many concurrent users to deal with.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-01-2011, 07:13 PM Re: Numbering posts based on datetime in MySQL
Average Talker

Posts: 19
Trades: 0
Is it less efficient than running another query completely for each row of my initial output? Eg:
Code:
while ($result = mysql_fetch_array($myOriginalQuery)) {
  $queryPostNumber = mysql_query("SELECT count(*) FROM forum WHERE datetime <= ."$result["datetime"];
Kent O'Matic is offline
Reply With Quote
View Public Profile
 
Old 09-01-2011, 08:25 PM Re: Numbering posts based on datetime in MySQL
Super Spam Talker

Posts: 879
Name: Paul W
Trades: 0
Quote:
Originally Posted by Kent O'Matic View Post
Is it less efficient than running another query completely for each row of my initial output? Eg:
Code:
while ($result = mysql_fetch_array($myOriginalQuery)) {
  $queryPostNumber = mysql_query("SELECT count(*) FROM forum WHERE datetime <= ."$result["datetime"];
The latter will rapidly become the less efficient as number of posts rises.

However, take a step back - is it important to you to have the sequential numbering ofposts or is it just an anchor point? If the latter, apply a post id on insertion to the table and use that in the anchor.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE


*** New:
Please login or register to view this content. Registration is FREE
PaulW is offline
Reply With Quote
View Public Profile
 
Old 09-01-2011, 11:02 PM Re: Numbering posts based on datetime in MySQL
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Code:
SELECT
  @row := @row + 1 AS row,
  f.*
FROM
  forum f,
  (SELECT @row := 0) r
WHERE
  f.topic_id = 9
ORDER BY
  f.datetime ASC
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 09-02-2011, 07:47 AM Re: Numbering posts based on datetime in MySQL
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Nice and in a single DB pass as well.

I'd better do some brushing up on MySQL
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 09-11-2011, 09:47 PM Re: Numbering posts based on datetime in MySQL
Average Talker

Posts: 19
Trades: 0
Quote:
Originally Posted by mgraphic View Post
Code:
SELECT
  @row := @row + 1 AS row,
  f.*
FROM
  forum f,
  (SELECT @row := 0) r
WHERE
  f.topic_id = 9
ORDER BY
  f.datetime ASC
I'll definitely give this a try, but I've done something similar (can't remember HOW similar, though. It may have been exactly that) and it spat out incorrect values for 'row.' Regardless of what I used for my ORDER BY clause it seemed to be assigning row numbers as if I were always ordering it by the index.

Thank you, either way.
Kent O'Matic is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Numbering posts based on datetime in MySQL
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.89780 seconds with 12 queries