Jump to content

Order By Date


pgrevents

Recommended Posts

SELECT * from table ORDER BY date DESC WHERE date > DATE_SUB(curdate(), INTERVAL 1 WEEK);

 

Depending on the order you want, the dates in you can use DESC (descending) or take it out which will, by default, make it ascending.

Link to comment
Share on other sites

Thanks for that I have tried implamenting that ill show you my code;

 

<?php
$query = mysql_query("SELECT * from programme ORDER BY expiry DESC WHERE expiry > (curdate(), INTERVAL 1 WEEK)") or die(mysql_error());
if(empty($query)){
print "NOT VALID";
}

while($row=mysql_fetch_array($query, MYSQL_ASSOC)){
?>
<?=$row['pid']?><br/>
Expiry : <?=$row['expiry']?><br/>
<hr>
<? } ?>

 

This throws back an error which is

 

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 'WHERE expiry > (curdate(), INTERVAL 1 WEEK)' at line 1

Link to comment
Share on other sites

Sorry, you have to reverse the order of the clauses, my fault.  Also, if you only want 1 username returned, you have to specify that with the LIMIT clause.

 

$query = mysql_query("SELECT * from programme WHERE expiry > (curdate(), INTERVAL 1 WEEK) ORDER BY expiry DESC LIMIT 1") or die(mysql_error());

Link to comment
Share on other sites

hi thanks for that but it displays the whole database but in date order is there away to show just the last seven days?

 

That's what this clause is supposed to filter out:

 

WHERE expiry >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK)

 

Try to echo it out and see what's actually being passed to mysql_query.

 

$sql = "SELECT * FROM programme WHERE expiry >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK) ORDER BY expiry DESC";
echo "Query => " . $sql;
$query = ($sql) or die(mysql_error());
if(empty($query)){

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.