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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:22:59+00:00 2026-05-27T23:22:59+00:00

I have following MySQL query: (SELECT c.Channel as name, count(*) as total_episode FROM (

  • 0

I have following MySQL query:

(SELECT c.Channel as name, count(*) as total_episode
 FROM (
     SELECT a.aid, a.vid 
     FROM videoItem v INNER JOIN aid2vid a USING(vid) 
     GROUP BY a.aid
 ) a1 INNER JOIN channelListingItem c USING(aid) 
 GROUP BY c.Channel
) 
UNION 
(SELECT c1.Channel as name, 0 as total_episode
 FROM channelListingItem c1 LEFT JOIN (
     SELECT c.Channel FROM (
         SELECT a.aid, a.vid 
         FROM videoItem v INNER JOIN aid2vid a USING(vid) 
         GROUP BY a.aid
     ) a1 INNER JOIN channelListingItem c USING(aid) 
     GROUP BY c.Channel
 ) c2 USING(Channel) 
 WHERE c2.Channel is null 
 GROUP BY name
);

Basically, what this statement does is to get the correct count episode in each channel & assign zero for channels w/o vid in consequent table (videoItem).

Note that

SELECT a.aid, a.vid
FROM videoItem v
INNER JOIN aid2vid a USING(vid)
GROUP BY a.aid

is duplicated twice and from explain this MySQL statement I don’t see MySQL re-use the query result.

+----+--------------+------------+------+----------+---------+---------+----------+------+---------------------------------+
| id | select_type  | table      | type | pos_keys | key     | key_len | ref      | rows | Extra                           |
+----+--------------+------------+------+----------+---------+---------+----------+------+---------------------------------+
|  1 | PRIMARY      | <derived2> | ALL  | NULL     | NULL    | NULL    | NULL     |  313 | Using temporary; Using filesort |
|  1 | PRIMARY      | c          | ALL  | idx_vid  | NULL    | NULL    | NULL     |  616 | Using where; Using join buffer  |
|  2 | DERIVED      | a          | ALL  | vid      | NULL    | NULL    | NULL     | 1015 | Using temporary; Using filesort |
|  2 | DERIVED      | v          | ref  | idx_vid  | idx_vid | 32      | db.a.vid |   10 | Using index                     |
|  3 | UNION        | c1         | ALL  | NULL     | NULL    | NULL    | NULL     |  616 | Using temporary; Using filesort |
|  3 | UNION        | <derived4> | ALL  | NULL     | NULL    | NULL    | NULL     |   28 | Using where; Not exists         |
|  4 | DERIVED      | <derived5> | ALL  | NULL     | NULL    | NULL    | NULL     |  313 | Using temporary; Using filesort |
|  4 | DERIVED      | c          | ALL  | idx_vid  | NULL    | NULL    | NULL     |  616 | Using where; Using join buffer  |
|  5 | DERIVED      | a          | ALL  | vid      | NULL    | NULL    | NULL     | 1015 | Using temporary; Using filesort |
|  5 | DERIVED      | v          | ref  | idx_vid  | idx_vid | 32      | db.a.vid |   10 | Using index                     |
|NULL| UNION RESULT | <union1,3> | ALL  | NULL     | NULL    | NULL    | NULL     | NULL |                                 |
+----+--------------+------------+------+----------+---------+---------+----------+------+---------------------------------+
11 rows in set (0.02 sec)

How do I refactor this MySQL statement? Also is there good refactor tool for MySQL statement?

Thanks.

  • 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-27T23:23:00+00:00Added an answer on May 27, 2026 at 11:23 pm

    This one seemed to work for me:

    select Channel as name,count(distinct a1.aid) as total_episode
    from channelListingItem c
    left join
    (
     select a.aid, a.vid 
     from videoItem v INNER JOIN aid2vid a USING(vid) 
    ) a1 on a1.aid = c.aid
    group by Channel;
    

    From what I can see the following query that you use twice in an inline view:

    SELECT a.aid, a.vid 
    FROM videoItem v INNER JOIN aid2vid a USING(vid) 
    GROUP BY a.aid
    

    Is being used to get a distinct list of aid and vid values that exist in both videoItem and aid2vid. I have replaced the GROUP BY in the inline view with a COUNT(DISTINCT) to achieve the same thing since you are not using any aggregate functions in the inline view part of the query.

    I think you do not need to split the query into two parts joined by a union i.e. part 1 to get episode counts > 0 and part 2 to get episode counts = 0. This can be achieved in one GROUP BY.

    Hope this helps!

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

Sidebar

Related Questions

I have the following MySQL query statement: SELECT * FROM table1 INNER JOIN table2
I have the following MYSQL query: SELECT categories.key, categories.name, count(*) as itemsCount FROM categories
I have the following MySql query: select t1.* from Table1 t1 inner join Table2
I have the following mysql query: EXPLAIN SELECT id, name, title, description, time FROM
I have the following MySQL query: SELECT (COUNT(id) * 5) AS total FROM content
I have the following MySQL query: SELECT pool.username FROM pool LEFT JOIN sent ON
I have the following mysql query: $manufacturers_query = select m.manufacturers_id, m.manufacturers_name, m.manufacturers_image, mi.manufacturers_url from
I have the following query to select users and their locations, etc from MySQL
I have the following MySQL query: SELECT value_1, ( SELECT value_4 FROM table_1 WHERE
I have the following MySQL Query: <div class=box id=settlement1> <? $query = SELECT name,

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.