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 837303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:07:23+00:00 2026-05-15T05:07:23+00:00

Consider following tables in MySQL database: entries: creator_id INT entry TEXT is_expired BOOL other:

  • 0

Consider following tables in MySQL database:

entries:
    creator_id INT
    entry TEXT
    is_expired BOOL
other:
    creator_id INT
    entry TEXT

userdata:
    creator_id INT
    name VARCHAR
    etc...

In entries and other, there can be multiple entries by 1 creator. userdata table is read only for me (placed in other database).

I’d like to achieve a following SELECT result:

+------------+---------+---------+-------+
| creator_id | entries | expired | other |
+------------+---------+---------+-------+
|      10951 |      59 |      55 |    39 | 
|      70887 |      41 |      34 |   108 | 
|      88309 |      38 |      20 |   102 | 
|      94732 |       0 |       0 |    86 |

… where entries is equal to SELECT COUNT(entry) FROM entries GROUP BY creator_id,
expired is equal to SELECT COUNT(entry) FROM entries WHERE is_expired = 0 GROUP BY creator_id and
other is equal to SELECT COUNT(entry) FROM other GROUP BY creator_id.

I need this structure because after doing this SELECT, I need to look for user data in the “userdata” table, which I planned to do with INNER JOIN and select desired columns.

I solved this problem with selecting “NULL” into column which does not apply for given SELECT:

SELECT
    creator_id,
    COUNT(any_entry) as entries,
    COUNT(expired_entry) as expired,
    COUNT(other_entry) as other
FROM (
    SELECT
        creator_id,
        entry AS any_entry,
        NULL AS expired_entry,
        NULL AS other_enry
    FROM entries
    UNION
    SELECT
        creator_id,
        NULL AS any_entry,
        entry AS expired_entry,
        NULL AS other_enry
    FROM entries
    WHERE is_expired = 1
    UNION
    SELECT
        creator_id,
        NULL AS any_entry,
        NULL AS expired_entry,
        entry AS other_enry
    FROM other
) AS tTemp
GROUP BY creator_id
ORDER BY
    entries DESC,
    expired DESC,
    other DESC
;

I’ve left out the INNER JOIN and selecting other columns from userdata table on purpose (my question being about combining 3 SELECTs into 1).

  • Is my idea valid? = Am I trying to use the right “construction” for this?
  • Are these kind of SELECTs possible without creating an “empty” column? (some kind of JOIN)
  • Should I do it “outside the DB”: make 3 SELECTs, make some order in it (let’s say python lists/dicts) and then do the additional SELECTs for userdata?

Solution for a similar question does not return rows where entries and expired are 0.

Thank you for your time.

  • 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-05-15T05:07:23+00:00Added an answer on May 15, 2026 at 5:07 am

    This should work (assuming all creator_ids appear in the userdata table.

    SELECT userdata.creator_id, COALESCE(entries_count_,0) AS entries_count, COALESCE(expired_count_,0) AS expired_count, COALESCE(other_count_,0) AS other_count
    FROM userdata
      LEFT OUTER JOIN
      (SELECT creator_id, COUNT(entry) AS entries_count_
       FROM entries
       GROUP BY creator_id) AS entries_q
        ON userdata.creator_id=entries_q.creator_id
      LEFT OUTER JOIN
      (SELECT creator_id, COUNT(entry) AS expired_count_
       FROM entries
       WHERE is_expired=0
       GROUP BY creator_id) AS expired_q
        ON userdata.creator_id=expired_q.creator_id
      LEFT OUTER JOIN
      (SELECT creator_id, COUNT(entry) AS other_count_
       FROM other
       GROUP BY creator_id) AS other_q
        ON userdata.creator_id=other_q.creator_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following HTML tables: <table id=myTable1> <tr><td><input type=text id=quantity1 name=quantity1 /></td></tr> <tr><td><input type=text
Consider the following table: create table temp ( name int, a int, b int
Consider the following database tables: Table messages with 13,000,000 rows (one row per message).
Consider the following two MySQL tables: companies ----------------------- id ticker 1 AA 2 AAPL
I've the following database my_table [id,name,address,phone] with a lot of entries and i would
Consider the following table: mysql> select * from phone_numbers; +-------------+------+-----------+ | number | type
Do splitting fields into multiple tables ever yield faster queries? Consider the following two
Consider the following table which has the fields - id (int) and date_created (datetime):
Consider the following code: @Entity @Table(name = a) public class A implements Serializable {
Please consider the following SQL Server table and stored procedure. create table customers(cusnum int,

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.