dbTalk Databases Forums  

Update Subquery Help

comp.databases comp.databases


Discuss Update Subquery Help in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
rh1200la@gmail.com
 
Posts: n/a

Default Update Subquery Help - 07-27-2007 , 03:45 PM






Hi there. I'm in need of assistance with an update query. I am doing
a data migration for a message board. All went fine except some of
the mappings didn't cross over. Here is what I need to do:

Tables:
Thread: threadid, firstpostid, lastpostid
Post: postid, threadid, dateline

I need to map the first post from post.postid to thread.firstpostid
and the last post from post.postid to thread.lastpost id.

I can get these values by two queries if I know the threadid

First Post = select postid, threadid from post where post.threadid =
1 order by FROM_UNIXTIME(dateline) asc limit 1

Last Post = select postid, threadid from post where post.threadid = 1
order by FROM_UNIXTIME(dateline) desc limit 1

Does anyone know how I can seamlesly update the Thread table with
these post id's respectively through one subquery?

I am using MySQL 5.x

Thanks in advance


Reply With Quote
  #2  
Old   
Tonkuma
 
Posts: n/a

Default Re: Update Subquery Help - 07-27-2007 , 11:53 PM






I'm not sure that MySQL 5.x accepts those sub-queries.

SELECT p1.threadid, p1.postid as firstpostid, p2.postid as lastpostid
FROM Post p1
, Post p2
WHERE FROM_UNIXTIME(p1.dateline)
= (SELECT MIN(FROM_UNIXTIME(dateline))
FROM Post
WHERE threadid = 1
)
AND FROM_UNIXTIME(p2.dateline)
= (SELECT MAX(FROM_UNIXTIME(dateline))
FROM Post
WHERE threadid = 1
);


Reply With Quote
  #3  
Old   
Tonkuma
 
Posts: n/a

Default Re: Update Subquery Help - 07-28-2007 , 12:41 AM



It seems that MySQL 5.x accept ORDER BY and LIMIT in a Subquery.
So, this might be worth to try.

SELECT threadid, firstpostid, lastpostid
FROM (SELECT postid, threadid
FROM Post
WHERE threadid = 1
ORDER BY FROM_UNIXTIME(dateline) ASC LIMIT 1
) p1(firstpostid, threadid)
, (SELECT postid
FROM Post
WHERE threadid = 1
ORDER BY FROM_UNIXTIME(dateline) DESC LIMIT 1
) p2(lastpostid);


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.