title: MySQL: Include COUNT of SELECT Query Results as a Column (Without Grouping)
body: <p>I have a simple report sending framework that basically does the following things:
  It performs a SELECT query, it makes some text-formatted tables based on the results, it sends an e-mail, and it performs an UPDATE query.</p>
  
  <p>This system is a generalization of an older one, in which all of the operations were hard coded. However, in pushing all of the logic of what I'd like to do into the SELECT query, I've run across a problem.</p>
  
  <p>Before, I could get most of the information for my text tables by saying:</p>
  
  <pre><code>SELECT Name, Address FROM Databas.Tabl WHERE Status='URGENT';
  </code></pre>
  
  <p>Then, when I needed an extra number for the e-mail, also do:</p>
  
  <pre><code>SELECT COUNT(*) FROM Databas.Tabl WHERE Status='URGENT' AND TimeLogged='Noon';
  </code></pre>
  
  <p>Now, I no longer have the luxury of multiple SELECT queries. What I'd like to do is something like:</p>
  
  <pre><code>SELECT Tabl.Name, Tabl.Address, COUNT(Results.UID) AS Totals
  FROM Databas.Tabl
  LEFT JOIN Databas.Tabl Results
      ON Tabl.UID = Results.UID
      AND Results.TimeLogged='Noon'
  WHERE Status='URGENT';
  </code></pre>
  
  <p>This, at least in my head, says to get a total count of all the rows that were SELECTed and also have some conditional.</p>
  
  <p>In reality, though, this gives me the "1140 - Mixing of GROUP columns with no GROUP columns illegal if no GROUP BY" error. The problem is, I don't want to GROUP BY. I want this COUNT to redundantly repeat the number of results that SELECT found whose TimeLogged='Noon'. Or I want to remove the AND clause and include, as a column in the result of the SELECT statement, the number of results that that SELECT statement found.</p>
  
  <p>GROUP BY is not the answer, because that causes it to get the COUNT of only the rows who have the same value in some column. And COUNT might not even be the way to go about this, although it's what comes to mind. FOUND_ROWS() won't do the trick, since it needs to be part of a secondary query, and I only get one (plus there's no LIMIT involved), and ROW_COUNT() doesn't seem to work since it's a SELECT statement.</p>
  
  <p>I may be approaching it from the wrong angle entirely. But what I want to do is get COUNT-type information about the results of a SELECT query, as well as all the other information that the SELECT query returned, in one single query.</p>
  
  <p>=== Here's what I've got so far ===</p>
  
  <pre><code>SELECT Tabl.Name, Tabl.Address, Results.Totals
  FROM Databas.Tabl
  LEFT JOIN (SELECT COUNT(*) AS Totals, 0 AS Bonus
             FROM Databas.Tabl
             WHERE TimeLogged='Noon'
             GROUP BY NULL) Results
       ON 0 = Results.Bonus
  WHERE Status='URGENT';
  </code></pre>
  
  <p>This does use sub-SELECTs, which I was initially hoping to avoid, but now realize that hope may have been foolish. Plus it seems like the COUNTing SELECT sub-queries will be less costly than the main query since the COUNT conditionals are all on one table, but the real SELECT I'm working with has to join on multiple different tables for derived information.</p>
  
  <p>The key realizations are that I can GROUP BY NULL, which will return a single result so that COUNT(*) will actually catch everything, and that I can force a correlation to this column by just faking a Bonus column with 0 on both tables.</p>
  
  <p>It looks like this is the solution I will be using, but I can't actually accept it as an answer until tomorrow. Thanks for all the help.</p>