Mysql - Multi Selects in a query

Asked

Viewed 54 times

1

I need to get all columns of a table and more a custom column in the same query, I tried this way:

SELECT (SELECT * FROM clans WHERE ID=1), (SELECT COUNT(*) FROM contas WHERE Clan=1) AS Membros;

You are giving error by trying to get more than one column by SELECT.

2 answers

0

Solved.

SELECT *, (SELECT COUNT(*) FROM contas WHERE Clan=1) AS Membros FROM clans WHERE ID=1;

0

You can use JOIN to make your query simpler:

SELECT  
    c.*,
    COUNT(cn.Clan) AS membros
FROM clans c
INNER JOIN contas cn
ON cn.Clan = c.ID
WHERE c.ID = 1
GROUP BY c.ID

Browser other questions tagged

You are not signed in. Login or sign up in order to post.