Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible to compare COUNT(*) from multiple rows on MySQL?
    primarykey
    data
    text
    <p>Hi I am working on a query that is using the below schema to find out how often users are doing searches by company (the app basically allowed you to do a search by company)</p> <pre><code>SEARCH_LOG ---------- date_of_search (DATETIME) company_id (INT) COMPANIES --------- id (INT) company_name (VARCHAR) (there are more columns but these are the relevant ones) </code></pre> <p>So I am running the following query:</p> <pre><code>SELECT company_name,COUNT(*) FROM companies LEFT OUTER JOIN search_log ON search_log.company_id=companies.id GROUP BY companies.id </code></pre> <p>This is great as it returns each company and the number of search performed, however I want to express these numbers as a percentage. My knee jerk reaction was to just run the following query seperately:</p> <pre><code>SELECT COUNT(*) FROM search_log </code></pre> <p>Grab that result and just do the division on the application side, however this seems really inefficient and I'd like to do it all in one query if possible (preferably without the use of a subquery) but have no idea how to get that information.</p> <p>Any help or guidance would be appreciated.</p> <p><strong>EDIT:</strong> Maybe I wasn't totally clear about what I'm after. Instead of getting results like:</p> <pre><code>COMPANY_NAME | COUNT(*) ----------------------- CompanyA | 1 CompanyB | 3 </code></pre> <p>I'd rather see:</p> <pre><code>COMPANY_NAME | COUNT(*) ----------------------- CompanyA | 25% CompanyB | 75% </code></pre> <p>Obviously the formatting isn't super important as 25,25%,0.25 are all usable.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload