Could you tell me how to write this sql, mysql database?

I now have two tables An and B, in which the primary key of table An is a foreign key in B. now I want to query the number of each primary key in A plus all the data in table A. how do you write this?

Mar.07,2022

SELECT
    G.count,A.*
FROM
    (
        SELECT
            COUNT(*) AS count,
            Id
        FROM
            B
        GROUP BY
            Id
    ) G
JOIN A ON G.Id = A.Id;

is not recommended to use associated queries. Sql is written separately. Easy to maintain.


SELECT COUNT (og. order_id ) AS num,o.* FROM va_order o LEFT JOIN va_order_goods og ON o. order_id = og. order_id GROUP BY o. order_id

Menu