PDA

View Full Version : query help


kamkam
02-04-2008, 10:22 AM
Hello;:confused:
I got 2 two tables which are Table 1(Thread) and Table 2(SubThread) from database.
Please to click the link (http://writeanything.org/AskQuestion.php) to see table 1 to table 4.

I want to union two tables of the table 1 and table 2 together and get the final result as
table 4. The table 3 is result of the union of the table 1 and table 2.

After union the two tables, i want to
Select ThreadId, Username and PostIme from Table 3 where username="kam12"
and
if the threadId equal each other, pick up the row with the biggest value of the postTime,
after above, order all the row by PostTime DESC



My query as following, but it does work, and it give me the following massage when i run my query.could any one help me, please.

Every derived table must have its own alias //error massage it give me

SELECT *
FROM

(select Id as threadId, userName, postTime from Thread
union
select threadId, userName, postTime from SubThread)

WHERE postTime
IN (
SELECT max( postTime )
FROM

(select Id as threadId, userName, postTime from Thread
union
select threadId, userName, postTime from SubThread)

WHERE userName = "kam12"
GROUP BY threadId
)order by postTime;

Fumigator
02-04-2008, 04:56 PM
A derived table is one that you create on the fly, as you've done with FROM (SELECT ...). You need to give each of those derived tables an alias name. FROM (SELECT ...) AS dtable1

kamkam
02-04-2008, 07:17 PM
Thanks a lot, it does work now