简体   繁体   中英

How do i write the condition in sql to get the required result SQL

I have 2 tables (1) Updates (2) Companies

Updates Table Columns: ID, Title, inserted_at, updated_at, revisions, published_at, archived_at, versions

Companies Table columns: id, name, host, email, inserted_at, updated_at, features.

How do I write a query to show how many posts have been made by a company.

What I know so far is I need to use COUNT in the query but how can I get the no. of updates by a company using that?

SELECT COUNT (column_name)
FROM TABLE (table_name)
condition??

Thanks in advance.

You will have to add a column "company_id" in Updates table which will reference the ID column in Companies table. You will need to add this column then only you can identify which update was made by which company.

So the new structure will be as follows

Companies (ID, name, host, email, inserted_at, updated_at, features)<br/>
Updates (ID, title, inserted_at, updated_at, revisions, published_at, archived_at, company_id)


Then use the following command to select the count of updates
SELECT u.company_id, c.name COUNT(u.company_id) FROM companies c, updates u, WHERE c.id =  u.company_id GROUP BY u.company_id, c.name;

Or if you want for all the companies then use the following command

SELECT u.company_id, c.name COUNT(u.company_id) FROM companies c, updates u, WHERE c.id = u.company_id GROUP BY u.company_id, c.name;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM