SA-MP Forums Archive
[MySQL] Getting data from 2 tables in 1 query - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: [MySQL] Getting data from 2 tables in 1 query (/showthread.php?tid=576127)



[MySQL] Getting data from 2 tables in 1 query - Deathlane - 01.06.2015

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
---


Re: [MySQL] Getting data from 2 tables in 1 query - Vince - 01.06.2015

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 



Re: [MySQL] Getting data from 2 tables in 1 query - Konstantinos - 01.06.2015

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.