我有这两个查询无法执行工会,得到#1248 - 每一个派生表必须有它自己的别名错误
select
t . *, events.event_time as last_time
from
events,
(
(
select
bonding.type,
bonding.global_id2 as target_post,
bonding.target_id as on_whose_post,
GROUP_CONCAT(bonding.shooter_id) as shooter_ids,
GROUP_CONCAT(bonding.what_global_id) as shooted_what,
MAX(bonding.what_global_id) as last,
'bonding' as flag
from
bonding
where
bonding.type = 1
and bonding.shooter_id in (select
`user2`
from
relation_table
where
`user1` = 192)
group by bonding.global_id2
)
union
(
select
bonding.type,
bonding.global_id2 as target_post,
bonding.target_id as on_whose_post,
GROUP_CONCAT(bonding.shooter_id) as shooter_ids,
GROUP_CONCAT(bonding.what_global_id) as shooted_what,
MAX(bonding.what_global_id) as last,
'bonding' as flag
from
bonding
where
bonding.type = 2
and bonding.shooter_id in (select
`user2`
from
relation_table
where
`user1` = 192)
group by bonding.global_id2
)
union
(
select
bonding.type,
bonding.global_id2 as target_post,
bonding.target_id as on_whose_post,
GROUP_CONCAT(bonding.shooter_id) as shooter_ids,
GROUP_CONCAT(bonding.what_global_id) as shooted_what,
MAX(bonding.what_global_id) as last,
'bonding' as flag
from
bonding
where
bonding.type = 5
and bonding.shooter_id in (select
`user2`
from
relation_table
where
`user1` = 192)
group by bonding.global_id2
)
union
(
select
bonding.type,
bonding.global_id2 as target_post,
bonding.target_id as on_whose_post,
GROUP_CONCAT(bonding.shooter_id) as shooter_ids,
GROUP_CONCAT(bonding.what_global_id) as shooted_what,
MAX(bonding.what_global_id) as last,
'bonding' as flag
from
bonding
where
bonding.type = 9
and bonding.shooter_id in (select
`user2`
from
relation_table
where
`user1` = 192)
group by bonding.global_id2
)
union
(
select
bonding.type,
bonding.global_id2 as target_post,
bonding.target_id as on_whose_post,
GROUP_CONCAT(bonding.shooter_id) as shooter_ids,
GROUP_CONCAT(bonding.what_global_id) as shooted_what,
MAX(bonding.what_global_id) as last,
'bonding' as flag
from
bonding
where
bonding.type = 10
and bonding.shooter_id in (select
`user2`
from
relation_table
where
`user1` = 192)
group by bonding.global_id2
)
)as t where events.global_id = t1.last
等一个: -
SELECT
post_stream.type,
post_stream.ref_global_id as target_post,
post_stream.user_id as on_whose_post,
post_stream.user_id as shooter_ids,
post_stream.ref_global_id as shooted_what,
post_stream.ref_global_id as last,
'stream' as flag,
events.event_time as last_time
FROM
post_stream,
events,
relation_table
WHERE
events.global_id = post_stream.ref_global_id
and post_stream.type IN (2 , 3, 7, 8)
AND post_stream.user_id = relation_table.user2
AND relation_table.user1 = 192
现在我需要执行两个查询的联合以获得组合结果,但它给出了 每个派生表都必须有它自己的别名错误,我应该为派生表放置一个别名,这两个查询在单独运行时运行时没有错误。
它要求你在你的第一个查询(每个'UNION(SELECT ...)'语句,包括第一个'SELECT'中别名子查询,你也可以用完全相同的方式别名第二个查询 – dash
我是一些如何能够解决问题,但这个查询需要40秒完成...... – manish
您需要停止使用'FROM a,b,c WHERE ... AND ...'表示法并使用显式的'FROM a JOIN b ON ... JOIN c ON ...'表示法,你需要知道其他的存在,这样你就可以读取在90年代写入的查询,当JOIN的支持并不总是可用时,你绝不应该写它自己 –