SELECT p.* FROM poll p LEFT JOIN poll_answer a ON p.id_poll = a.id_poll LEFT JOIN poll_answer_user au ON au.id_poll = p.id_poll WHERE `archive` = 1 AND `hide` = 0 OR `enddate` < CURDATE() 

It is necessary to count the number of matches, when glueing poll_answer_user and add a new output column with the number of matches.

    1 answer 1

    If I understand correctly, you want to count the number of responding users for each survey. Literally:

    SELECT id_poll, COUNT(*) AS cnt FROM poll_answer_user GROUP BY id_poll

    To add fields from the polls table, use this as a sub-query:

    SELECT p.*, IFNULL(gau.cnt, 0) AS cnt FROM poll AS p LEFT JOIN ( SELECT id_poll, COUNT(*) AS cnt FROM poll_answer_user GROUP BY id_poll ) AS gau USING(id_poll) WHERE p.`archive` = 1 AND (p.`hide` = 0 OR p.`enddate` < CURDATE())

    By the way, be careful with the combination of AND and OR. If you do not take into account the priority of operations, you can get an unexpected result. Brackets will indicate exactly what is required.