I'm trying to run an update statement that'll get the smallest value
from one table and insert it into another. The following update
statement does this (I think), but doesn't work on any version of MySQL
that I have access to (4.0 and 3.23):
UPDATE phpbb_users SET (user_firstpost) = SELECT MIN(post_time) FROM
phpbb_posts WHERE phpbb_posts.poster_id = phpbb_users.user_id;
I think the reason it doesn't work is because it uses a subquery, as
well.
The following update - which does work in 4.0 unlike the previous one -
seems to do the same thing:
UPDATE phpbb_users AS u, phpbb_posts AS p1
LEFT OUTER JOIN
phpbb_posts AS p2
ON p1.poster_id = p2.poster_id AND
p2.post_time < p1.post_time
INNER JOIN
phpbb_posts AS p3
ON p3.post_time = p1.post_time
SET u.user_firstpost=p3.post_time
WHERE p2.post_time IS NULL AND
p1.poster_id = u.user_id;
Unfortunately, this update doesn't work in MySQL 3.23. When I try to
run it, I get the following error:
You have an error in your SQL syntax near 'AS u, posts AS p1 LEFT OUTER
JOIN posts AS p2 ON p1.poster_id = p2.p' at line 1
So.. any ideas as to what's wrong with the second update?
Alternatively, any ideas as to how to do the first update in a way that
works with MySQL 3.23?
--CELKO-- - 24 Mar 2005 17:13 GMT
>> UPDATE phpbb_users SET (user_firstpost) = SELECT MIN(post_time) FROM
phpbb_posts WHERE phpbb_posts.poster_id = phpbb_users.user_id; <<
Since MySQL is soooo far from being a SQL, I am not sure, but doesn't
putting a column name in parens turn it into an expression? And don't
subqueies have to be in parens?
UPDATE phpbb_users
SET user_firstpost
= (SELECT MIN(post_time)
FROM phpbb_posts
WHERE phpbb_posts.poster_id = phpbb_users.user_id);
Your second example makes absolutely no sense in SQL, so I cannot evern
read it. Have you thought about getting a product that is actually an
implementation of SQL instead? Ingres is open source now, for example.