2

SQL 新手。我正在尝试对两个派生表执行 LEFT JOIN,但我收到一个错误Every Derived Table must have an alias.,我确实为它们提供了别名,但它仍然给出相同的错误消息。以下是查询:

SELECT * FROM 
    (SELECT first_name 
        from users 
        inner join messages 
        on (users.id=messages.from_user_id)
        where to_user_id=1 
        group by first_name order by message_date) AS DATAONE 
            LEFT JOIN
                (select * from 
                    (SELECT count(message_id),u.id,first_name,message_subject,message_status 
                    from users u 
                    inner join messages m 
                    on (u.id=from_user_id)
                    where to_user_id=1 and message_status = 2 
                    group by first_name order by message_date) as datatwo) 
        on (dataone.first_name = datatwo.first_name)

请指教。

4

1 回答 1

5

你忘了一个:

SELECT * 
FROM     
(
   SELECT first_name 
   from users 
   inner join messages on users.id = messages.from_user_id
   where to_user_id = 1 
   group by first_name 
   order by message_date
) AS DATAONE 
LEFT JOIN
(
   select * 
   from 
   (
       SELECT
         count(message_id),
         u.id,
         first_name,
         message_subject,
         message_status 
       from users u 
       inner join messages m on u.id = from_user_id
       where to_user_id     = 1
         and message_status = 2 
       group by first_name order by message_date
   ) as datatwo
) AS Sub   -- <------- You forgot to put an alias here
on dataone.first_name = Sub.first_name

或者:

你忘了一个:

SELECT * 
FROM     
(
   SELECT first_name 
   from users 
   inner join messages  on (users.id=messages.from_user_id)
   where to_user_id=1 
   group by first_name order by message_date
) AS DATAONE 
LEFT JOIN
(
       SELECT
         count(message_id),
         u.id,
         first_name,
         message_subject,
         message_status 
       from users u 
       inner join messages m  on (u.id=from_user_id)
       where to_user_id     = 1 
         and message_status = 2 
       group by first_name
       order by message_date
) as datatwo    -- <------- You forgot to put an alias here
on (dataone.first_name = datatwo.first_name)
于 2013-03-25T07:16:05.270 回答