Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-12-2012, 02:01 PM
PM User |
#1
New Coder
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
mysql Select timestamp
Hello
I am trying to select must commented thread of the week, and I have tried a few ways, but cant seem to get it working.
Hope you can help...
PHP Code:
$sql = "SELECT thread_id,thread_name,comment_timestamp, COUNT(thread_name) AS p_count FROM comment WHERE comment_timestamp >= CURRENT_timestamp - INTERVAL 7 DAYS GROUP BY thread_name ORDER BY p_count DESC LIMIT 0,1" ;
$result = mysql_query ( $sql , $conn ) or die( mysql_error ());
while ( $newArray = mysql_fetch_array ( $result ))
{
$thread_name = $newArray [ thread_name '];
$thread_id = $newArray[thread_id' ];
$p_count = $newArray [ 'p_count' ];
echo "<center><a href=thread_view.php?thread_id=$thread_id'>$thread_name</a><br>Comments ($p_count)<br><br></center>" ;
}
01-12-2012, 09:41 PM
PM User |
#2
Regular Coder
Join Date: Jun 2010
Posts: 239
Thanks: 50
Thanked 6 Times in 6 Posts
Did you write the code yourself? I can see two syntax errors,
Try this:
PHP Code:
$sql = "SELECT thread_id,thread_name,comment_timestamp, COUNT(thread_name) AS p_count FROM comment WHERE comment_timestamp >= CURRENT_timestamp - INTERVAL 7 DAYS GROUP BY thread_name ORDER BY p_count DESC LIMIT 0,1" ; $result = mysql_query ( $sql , $conn ) or die( mysql_error ()); while ( $newArray = mysql_fetch_array ( $result )) { $thread_name = $newArray [ 'thread_name' ]; $thread_id = $newArray [ 'thread_id' ]; $p_count = $newArray [ 'p_count' ]; echo "<center><a href=thread_view.php?thread_id=$thread_id'>$thread_name</a><br>Comments ($p_count)<br><br></center>" ; }
01-15-2012, 10:23 AM
PM User |
#3
New Coder
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
sorry didnt work
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DAYS GROUP BY thread_name ORDER BY p_count DESC LIMIT 0,1' at line 1
01-15-2012, 11:17 AM
PM User |
#4
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
I believe MySQL is like PHP. That should be +/- DAY not DAYS for the interval.
01-15-2012, 11:19 AM
PM User |
#5
New Coder
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
I have no error now, but it is not displaying anything
01-15-2012, 11:27 AM
PM User |
#6
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Quote:
Originally Posted by
soupy1985
I have no error now, but it is not displaying anything
And using
print mysql_num_rows($result); shows how many rows?
01-15-2012, 11:32 AM
PM User |
#7
New Coder
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
nothing
01-15-2012, 11:36 AM
PM User |
#8
New Coder
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
0 sorry
01-15-2012, 11:40 AM
PM User |
#9
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Quote:
Originally Posted by
soupy1985
nothing
Then you have no records matching the criteria you have specified.
The reason is your selection of records. My assumption is you intend to find the most recent reply to the most popular topic in the last 7 days. Problem is you are selecting the most popular topics which
started within the past 7 days.
The MySQL guys can help you much better than I can with this, but I'd assume if you need to find the most recent reply based on popularity you will need to get the max date as well. So try this:
Code:
SELECT thread_id, thread_name, comment_timestamp, count(thread_name) as p_count FROM comment GROUP BY thread_name HAVING MAX(comment_timestamp) >= NOW() - INTERVAL 7 DAY ORDER BY p_count DESC LIMIT 0,1;
01-15-2012, 11:42 AM
PM User |
#10
New Coder
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
what i want is the most commented thread this week.
Thanks
01-15-2012, 11:44 AM
PM User |
#11
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Quote:
Originally Posted by
soupy1985
what i want is the most commented thread this week.
Thanks
And the result of the above provide?
01-15-2012, 11:45 AM
PM User |
#12
New Coder
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
doesnt work, id i take out:
Quote:
HAVING MAX(comment_timestamp) >= NOW() - INTERVAL 7 DAY
It gets the top thread of all time, so the rest of the code works, its just the timestamp part we are having probs with?
01-15-2012, 11:47 AM
PM User |
#13
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Is your comment_timestamp and actual datetime datatype?
01-15-2012, 11:50 AM
PM User |
#14
New Coder
Join Date: Jan 2012
Posts: 26
Thanks: 5
Thanked 0 Times in 0 Posts
comment_timestamp is int(11) in db and it is added with time() when a commented is added. If that makes sence lol.
01-15-2012, 11:54 AM
PM User |
#15
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Then you cannot use dateinterval on it directly. You need to convert it first using FROM_UNIXTIME to convert the integer to a datetime.
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 09:34 AM .
Advertisement
Log in to turn off these ads.