Multiple query data into single HTML Table (PHP, MySQL)

前端 未结 3 1939
夕颜
夕颜 2021-01-23 11:18

Here is my current code. I\'m trying to display data from three separate queries into a single table with multiple columns. Is my while statement wrong here? It\'s printing 1 ta

3条回答
  •  灰色年华
    2021-01-23 11:36

    This will require opening three separate connections to MySQL aтв running three queries, each in its own connection.

    You better rewrite your query as this

    SELECT  user_id,
            (
            SELECT  COUNT(*)
            FROM    posts p
            WHERE   p.user_id = u.user_id
                    AND p.date >= '2010-06-01'
                    AND p.date < '2010-07-01'
            ) AS june_count,
            (
            SELECT  COUNT(*)
            FROM    posts p
            WHERE   p.user_id = u.user_id
                    AND p.date >= '2010-07-01'
                    AND p.date < '2010-08-01'
            ) AS july_count,
            (
            SELECT  COUNT(*)
            FROM    posts p
            WHERE   p.user_id = u.user_id
                    AND p.date >= '2010-08-01'
                    AND p.date < '2010-09-01'
            ) AS aug_count
    FROM    users u
    

提交回复
热议问题