1

I've been trying to get 4 or more COUNT(*) queries from different tables into one query.

Is this even possible or do I have to make four different queries?

This is my code:

try (final PreparedStatement sel = conn.prepareStatement("SELECT COUNT(*), SUM(coins) FROM User");
            final PreparedStatement sel2 = conn.prepareStatement("SELECT COUNT(*) FROM Friends");
            final PreparedStatement sel3 = conn.prepareStatement("SELECT COUNT(*) FROM Clans");
            final PreparedStatement sel4 = conn.prepareStatement("SELECT COUNT(*) FROM ClanMembers")) {
        try (final ResultSet rs = sel.executeQuery(); final ResultSet rs2 = sel2.executeQuery(); final ResultSet rs3 = sel3.executeQuery()) {
            rs.last();
            rs2.last();
            rs3.last();
            return new Object[] { rs.getInt(1), rs.getInt(2), rs2.getInt(1) / 2, rs3.getInt(1) };
        }
}
2
  • UNION ALL, or JOIN the sub-queries. Commented Sep 8, 2017 at 7:00
  • Using UNION ALL is the simplest solution to me but you have one query with 2 column ... so this will not work. so using sub-queries is one solution. FYI : You could use a Statement if you don't pass any parameter. Commented Sep 8, 2017 at 7:03

3 Answers 3

6
SELECT COUNT(*), SUM(coins) FROM User
union all
SELECT COUNT(*), 0 FROM Friends
union all
SELECT COUNT(*), 0 FROM Clans
union all
SELECT COUNT(*), 0 FROM ClanMembers
...

Amount and types of columns must be the same for all unioned queries

Answer updated to show how to combine the queries.

Sign up to request clarification or add additional context in comments.

3 Comments

Look the first query ;) there is a COUNT and a SUM. So your statement is actually telling he can't use this. But that match the quersion title ... so I guess this is valid ... I give you that !
Thanks! I noticed you added some zeros. Probably so the union works out. Will I have to ignore the zeros while going through the result set? Or won't they appear in my resultset?
To union queries they must have the same amount of columns and types. So if the first query has 2 columns then the second must also have 2 columns - that's why 0 value for the second column was added
4

Just give another optional solution (i can't comment yet so i have to write as answer)

StanislavL's answer is a nice answer, you can use it, it will return (n) row

but just in case if you need just 1 row you can try this

SELECT q1.count1,q1.coins,q2.count2, q3.count3, q4.count4
from (select (COUNT(*) count1, SUM(coins) coins, 1 id FROM User, 1 var) q1
inner join (SELECT COUNT(*) count2, 1 id FROM Friends) q2 on q1.id=q2.id
inner join (SELECT COUNT(*) count3, 1 id FROM Clans) q3 on q1.id=q3.id
inner join (SELECT COUNT(*) count4, 1 id FROM ClanMembers) q4 on q1.id=q4.id

Comments

0

Well, it seems that I missed the obvious answer.

This solved my problem:

SELECT COUNT(*) FROM User union all SELECT COUNT(*) FROM Friends union all SELECT COUNT(*) FROM Clans union all SELECT COUNT(*) FROM ClanMembers union all SELECT SUM(coins) FROM User ...

Comments

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.