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)
请建议。
答案 0 :(得分: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)