1

In the database I am working on, there are 14 news tables (table1, table2 .... table14) of different cities with exact same structure. Some of the columns are: id, author, date, news_title, news_body

There are different authors who post in relevant city table. Now I want to know how many news has been posted by each author today in all 14 tables.

Here is the format of output:

author | num_of_post
bob          5
peter        12

Remember, there are 14 tables (this is the problem)

8
  • 4
    You can use union all, but at least you've learned something about bad database design. Commented Jan 31, 2017 at 13:18
  • 1
    use JOINS. This schema is very badly designed btw. sitepoint.com/understanding-sql-joins-mysql-database Commented Jan 31, 2017 at 13:19
  • 1
    Does MySQL have Views? or an equivalent? If so, create a view that includes all 14 tables, (use Union, or full joins). and drive your query against the View. But why is this data spread across 14 tables instead of being in just one with a column named city? Not that that is intrinsically wrong, but there are only a few good reasons to do that. Commented Jan 31, 2017 at 13:21
  • @CharlesBretana I have not designed the database Commented Jan 31, 2017 at 13:23
  • 1
    How big it is does not affect this. If you do not have the authority, and are unable or unwilling to approach the individual who does, well, then, my sympathies. Commented Jan 31, 2017 at 14:20

1 Answer 1

1

As @Grodon already stated you can use union like this:

select author, count(id) as num_of_post from table1 group by author
union
select author, count(id) as num_of_post from table2 group by author
union
select author, count(id) as num_of_post from table3 group by author

And so on


Update:

Because you have records from 1 author in multiple tables, you can group the union result again.

select author, sum(num_of_post) from (
 select author, count(id) as num_of_post from table1 group by author
 union
 select author, count(id) as num_of_post from table2 group by author
 union
 select author, count(id) as num_of_post from table3 group by author) a group by author
Sign up to request clarification or add additional context in comments.

6 Comments

but it gives 2 or more rows of same author, like this: author | num_of_post bob 5 bob 2 peter 3
if you could add some dummy data I could write the exact query, but according to your comment I now understand that 1 author can have records in multiple tables
Yes all authors can post in all the tables, therefore it is giving result of same author multiple times. I want to know the total num of post by all authors in all tables. Suppose author1 has posted 1 post in table1, 5 post in table2 and 0 post in all other tables then total num of post by author1 is 6
could you either try the updated version or could you add some dummy data so I can write the exact query?
I think it is working, but let me check. Will reply you later
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.