[MySQL] Getting data from 2 tables in 1 query
#1

This is my query:

Код:
SELECT Count(*) AS Accounts, SUM(CASE WHEN Admin > 0 THEN 1 ELSE 0 END) AS Admins, SUM(CASE WHEN VIP = 1 THEN 1 ELSE 0 END) AS VIPs FROM Accounts
^ This gets the number of rows, Admins & VIPs.

I want to merge this query with the query above

Код:
SELECT COUNT(*) As Bans FROM Bans
^ This gets the number of rows

I know there's a way, but I just can't figure out.

I want the result to be like this:

---
Accounts Admins VIPs Bans
x_________x_______x____x
---
Reply
#2

You can only join data from different tables if you have a key to compare with. So no, I don't think that is possible. You can use union to put them beneath each other, but that's about it.
PHP код:
select 'accounts'count(*) from accounts
union
select 
'admins'count(*) from accounts where admin 0
union
select 
'vips'count(*) from accounts where vip 0
union
select 
'bans'count(*) from bans 
Reply
#3

It can be done by having a subquery, no need of UNION.

pawn Код:
SELECT Count(*) AS ACCOUNTS, SUM(CASE WHEN Admin > 0 THEN 1 ELSE 0 END) AS ADMINS, SUM(CASE WHEN VIP = 1 THEN 1 ELSE 0 END) AS VIPS, (SELECT COUNT(*) FROM Bans) AS BANS FROM Accounts;
EDIT: By the way have a different name for your alias, Deathlane. I'll have them in caps, just in case.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)