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

  • Home
  • SEARCH
  • 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 8229789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:54:53+00:00 2026-06-07T16:54:53+00:00

I have this two queries: SELECT `a`.*, `b`.`id` AS host_id, SUM(CASE WHEN c.event_id IS

  • 0

I have this two queries:

SELECT `a`.*, `b`.`id` AS host_id, SUM(CASE WHEN c.event_id IS NOT NULL THEN 1 ELSE 0 END) AS count_joins, SUM(CASE WHEN c.event_id IS NOT NULL AND c.user_id = 0 THEN 1 ELSE 0 END) AS joined
FROM (`events` AS a)
INNER JOIN `users` AS b ON `b`.`id` = `a`.`host_id`
LEFT JOIN `joins` AS c ON `c`.`event_id` = `a`.`id`
WHERE `a`.`date` > '2012-07-12 11:51:34'
GROUP BY `a`.`id`
ORDER BY `a`.`date` ASC
LIMIT 20

and

SELECT `b`.`id`, `b`.`first_name`, `b`.`last_name`, `b`.`email`, `b`.`username`, `b`.`thumbnail`
FROM (`joins` AS a)
INNER JOIN `users` AS b ON `b`.`id` = `a`.`user_id`
WHERE `a`.`event_id` =  '1'
AND `a`.`user_id` != 0
ORDER BY  RAND()
LIMIT 8

The first one get all events, then, using a foreach loop, I get the joins of each event with the second query.

My question is, how can I do all that with a single query?

Here is my schema:

schema

I’d like to return a multidimensional array, for example:

Array
(
    [0] => Array
        (
            [id] => 1
            [title] => Title
            [description] => DescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescriptionDescription
            [segment] => title
            [thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg
            [cover] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg
            [locale] => Locale
            [address] => Rua Afonso Pena, 22, Tijuca
            [list] => 0
            [date] => 2013-10-10 10:10:10
            [created] => 
            [host_id] => 1
            [count_joins] => 5
            [joined] => 0
            [joins] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [first_name] => Giovanna
                            [last_name] => Carneiro
                            [email] => gigi@gmail.com
                            [username] => gigi
                            [thumbnail] => 1b6453892473a467d07372d45eb05abc2031647a.jpg
                        )

                    [1] => Array
                        (
                            [id] => 5
                            [first_name] => Júlio
                            [last_name] => César
                            [email] => jujuba@gmail.com
                            [username] => jujuba
                            [thumbnail] => ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4.jpg
                        )

                    [2] => Array
                        (
                            [id] => 3
                            [first_name] => Claudio
                            [last_name] => Cardozo
                            [email] => cazo66@gmail.com
                            [username] => cazo
                            [thumbnail] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
                        )

                    [3] => Array
                        (
                            [id] => 1
                            [first_name] => Claudius
                            [last_name] => Ibn
                            [email] => ibnclaudius@gmail.com
                            [username] => ibnclaudius
                            [thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg
                        )

                    [4] => Array
                        (
                            [id] => 2
                            [first_name] => Elza
                            [last_name] => Virginia
                            [email] => elza.mosqueira@gmail.com
                            [username] => elzavirginia
                            [thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg
                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [title] => Another Title
            [description] => Description
            [segment] => another-title
            [thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpeg
            [cover] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpeg
            [locale] => Locale
            [address] => Travessa Nestor Vitor, 117, Tijuca
            [list] => 0
            [date] => 2013-10-10 10:10:10
            [created] => 
            [host_id] => 3
            [count_joins] => 5
            [joined] => 0
            [joins] => Array
                (
                    [0] => Array
                        (
                            [id] => 1
                            [first_name] => Claudius
                            [last_name] => Ibn
                            [email] => ibnclaudius@gmail.com
                            [username] => ibnclaudius
                            [thumbnail] => 356a192b7913b04c54574d18c28d46e6395428ab.jpeg
                        )

                    [1] => Array
                        (
                            [id] => 4
                            [first_name] => Giovanna
                            [last_name] => Carneiro
                            [email] => gigi@gmail.com
                            [username] => gigi
                            [thumbnail] => 1b6453892473a467d07372d45eb05abc2031647a.jpg
                        )

                    [2] => Array
                        (
                            [id] => 3
                            [first_name] => Claudio
                            [last_name] => Cardozo
                            [email] => cazo66@gmail.com
                            [username] => cazo
                            [thumbnail] => 77de68daecd823babbb58edb1c8e14d7106e83bb.jpg
                        )

                    [3] => Array
                        (
                            [id] => 5
                            [first_name] => Júlio
                            [last_name] => César
                            [email] => jujuba@gmail.com
                            [username] => jujuba
                            [thumbnail] => ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4.jpg
                        )

                    [4] => Array
                        (
                            [id] => 2
                            [first_name] => Elza
                            [last_name] => Virginia
                            [email] => elza.mosqueira@gmail.com
                            [username] => elzavirginia
                            [thumbnail] => da4b9237bacccdf19c0760cab7aec4a8359010b0.jpg
                        )

                )

        )

)

The first query return the events, the second the joins. I’d like to return everything, if possible, with only ONE query.

  • 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-07T16:54:55+00:00Added an answer on June 7, 2026 at 4:54 pm

    One of the issues with pure SQL is that you can’t create nested results in a single query. You can only return flat records with all the fields. You would then use your application layer to parse the records into nested rows.

    If you are using ORMs like linq or Hibernate, all the work goes into the background.

    You can get a result like:

    events.id| events.title| events.locale| users.id| users.first_name| users.last_name

    which, in your case, would return 10 distinct records, where fields from event would be duplicated in each record. Your application server would then parse that into the nested array.

    I’ve written queries both ways, where I get a flat record, the way that I’ve described it, and parsed it on the application server, and where I do an initial query for the events, and separate query for each event, again from the application server. They both work, it’s just a question of whether it’s simpler/faster in your case to get the events, then their users, or to get all the data and then parse it.

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

Sidebar

Related Questions

I have two queries use [DatabaseAA] select * ,DATEDIFF(MINUTE,SomeDate,'3/28/2012 12:52:25 PM +00:00') This passes,
i have two sql queries. select EmployeeID,FName from [Mydatabase].[dbo].EMPLOYEE_TABLE where EmployeeID not in (select
I have two queries serving the same purpose. SELECT * FROM #user INNER JOIN
I have two queries. The first will return multiple rows: SELECT parent_entry_id,child_entry_id FROM exp_playa_relationships
I have two queries that I run in the same table: SELECT id, COUNT(up)
I have a simple stored proc with two queries joined with a union: select
I have two sql queries: $query0=mysql_query(SELECT caseNumber FROM shipped_data WHERE palette='P0' AND shipInvoiceNumber='2011/229'); $query1=mysql_query(SELECT
Can someone explain this to me? I have two queries below with their results.
How can I make this two queries in one ? $query = SELECT *
I'm using parameterized queries with PHP I have the following two queries: SELECT username,question_text

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.