Jump to content

Unable To Perform Union, Getting #1248 - Every Derived Table Must Have Its Own Alias Error


Recommended Posts

I have these two queries

 

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

 

and other one :-

 

 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, 
		AND post_stream.user_id = relation_table.user2
		AND relation_table.user1 = 192

 

now I need to perform a join on both the queries to get combined result, but it is giving

Every derived table must have its own alias error, where I should put an alias for derived table these two queries are running with no errors when run separately.

Edited by manishk3008

Each of your table subqueries requires an alias, just like the message says.

FROM
(
  SELECT blah
  FROM yadayada
) as aliaseName 1
UNION
(
   SELECT more
   FROM another
) as aliasName2

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.