Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8888473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:02:52+00:00 2026-06-14T22:02:52+00:00

I have these two queries select t . *, events.event_time as last_time from events,

  • 0

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 where events.global_id = t1.last

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, 8)
        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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:02:55+00:00Added an answer on June 14, 2026 at 10:02 pm

    The first query is about 116 lines long as formatted, and contains a 5-way UNION sub-query. Those 5 sub-queries appear to be identical apart from one value in the WHERE clause. This rewrite dramatically simplifies the SQL to:

    SELECT t.type, t.target_post, t.on_whose_post, t.shooter_ids, t.shooted_what,
           t.last, t.flag, events.event_time AS last_time
      FROM events JOIN
           (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 IN (1, 2, 5, 9, 10)
               AND bonding.shooter_id IN (SELECT user2 FROM relation_table WHERE user1 = 192)
             GROUP BY bonding.global_id2
            ) AS t
         ON events.global_id = t1.last
    

    This will be much easier to combine with the second query. With further revision, I’d probably remove the bonding. prefixes since the only table in the main sub-query is bonding.

    The second query should be rewritten using JOIN notation too:

    SELECT p.type          AS type,
           p.ref_global_id AS target_post,
           p.user_id       AS on_whose_post,
           p.user_id       AS shooter_ids,
           p.ref_global_id AS shooted_what,
           p.ref_global_id AS last,
           'stream'        AS flag,
           e.event_time    AS last_time
      FROM post_stream    AS p
      JOIN events         AS e ON e.global_id = p.ref_global_id
      JOIN relation_table AS r ON p.user_id = r.user2
     WHERE r.user1 = 192
       AND post_stream.type IN (2 , 3, 7, 8)
    

    Questions:

    1. Are you sure that on_whose_post and shooter_ids should be from the same column?
    2. Are you sure that shooted_what and last should be from the same column?

    There could be valid (and not too far-fetched) reasons for doing that — but it isn’t immediately obvious.

    Unfortunately, we’ve not been told how to join the data from the first query above with the second query. There seem to be quite a lot of columns in common; only the OP can determine what’s required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have combined these two queries: SELECT `ad_general`.`id`, `ad_general`.`status`, `ad_general`.`category`, `ad_general`.`type`, `ad_general`.`specification`, `ad_general`.`m2`, `ad_general`.`price`,
I have these two queries to test my output SELECT DISTINCT( providerId ), SUM((
This is for Android SQLite. I have two queries like this: select * from
If I have two queries SELECT Id, Forename, Surname FROM Person WHERE PersonName Like(‘%frank%’)
I have two seperate queries: SELECT `ad_general`.`id` FROM (`ad_general`) WHERE `ad_general`.`city` = '708' ORDER
I have two different queries: SELECT PB_BANK_CODE, PB_BANK_NAME FROM GLAS_PDC_BANKS WHERE PB_COMP_CODE='1' AND PB_BANK_CODE='025'
I have two queries: <cfquery name=getChairReview datasource=#application.dsn#> SELECT* FROM eval_reviews WHERE faculty = <cfqueryparam
I have a table of login events coming from Active Directory. One type of
I have these two SQL queries at the top of every page on my
I have run these two queries that look like they do the same thing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.