PDA

View Full Version : [SQL] Smallest value in a column


neverstop
01-03-2008, 04:35 AM
Hi,

I have a cron script that looks like so:

<?php

error_reporting(E_ALL & ~E_NOTICE);
if (!is_object($vbulletin->db))
{
exit;
}

$l = $vbulletin->db->query_first("
SELECT links.linkid
FROM adv_links AS links
LEFT JOIN adv_links_cfields_entries AS cfields
ON (links.linkid=cfields.lid)
WHERE links.valid=0
AND cfields.field5='approved'
");
$vbulletin->db->query_write("
UPDATE adv_links
SET valid=1, dateline=UNIX_TIMESTAMP()
WHERE linkid=".$l['linkid']
);
?>

The part I want to change is "WHERE linkid=".$l['linkid']" .

What I want it to do is "WHERE dateline=smallest value".

(I want to update the row with the oldest timestamp to timenow.)

So I imagine I would have to add links.dateline to the first query. But how can I change that query to update the row with the oldest timestamp. I found some reading regarding MIN() but I am not sure how to implement it here.

Cheers,
Ian