title: MariaDB comparing number count from two tables
body: <p>I'm trying to compare the total <code>COUNT</code> of one value in a table with the total <code>COUNT</code> of another, I'm just not sure how to do it properly. </p>
  
  <p>If a <code>SalesID</code> total <code>COUNT</code> in <code>total_sales</code> table is higher than <code>fid</code> total <code>COUNT</code> in <code>total_leads</code> table then I would like to output those values. </p>
  
  <p>Tables:</p>
  
  <pre><code>CREATE TABLE total_leads
      (`uid` int, `tstamp` int, `fid` int, `phone` varchar(2))
  ;
  
  INSERT INTO total_leads
      (`uid`, `tstamp`, `fid`, `phone`)
  VALUES
      (1, 1299795364, 1525, '""'),
      (1, 1309854563, 4031, '""'),
      (1, 1309854562, 3134, '""'),
      (5, 1280752536, 18, '""'),
      (5, 1280942244, 219, '""'),    
      (5, 1309862562, 1234, '""'),
      (5, 1480752536, 184, '""'),
      (5, 1520942244, 119, '""')
  ;
  
  CREATE TABLE total_sales
      (`UserID` int, `SalesID` int, `Time` varchar(8))
  ;
  
  INSERT INTO total_sales
      (`UserID`, `SalesID`, `Time`)
  VALUES
      (1, 172, '12:57:43'),
      (1, 3563, '15:59:49'),
      (1, 9508, '01:46:47'),
      (5, 18935, '07:26:07'),
      (5, 19378, '08:06:41'),
      (1, 144, '11:52:41'),
      (1, 9248, '02:43:40'),
      (1, 3423, '14:54:45'),
      (5, 11935, '03:21:06'),
      (1, 1448, '05:02:24')
  ;
  </code></pre>
  
  <p>MySQL Commands:</p>
  
  <pre><code>SELECT x.uid, COUNT(*), COUNT(DISTINCT x.fid)
  FROM total_leads AS x
  WHERE x.uid BETWEEN 1 AND 5
  GROUP BY x.uid;
  
  SELECT ud.UserId, COUNT(*), COUNT(DISTINCT ud.SalesID)
  FROM total_sales AS ud
  WHERE ud.UserId BETWEEN 1 AND 5  
  GROUP BY ud.UserID;
  </code></pre>
  
  <p>It returns (total_leads, total_sales):</p>
  
  <pre><code> uid    COUNT(*)    COUNT(DISTINCT x.fid)
    1        3               3
    5        5               5
  
  UserId  COUNT(*)    COUNT(DISTINCT ud.SalesID)
    1        7               7
    5        3               3
  </code></pre>
  
  <p>So I'm hoping to get a result such as:</p>
  
  <pre><code>UserId   Sales           Leads   Dif
    1        7               3      4
  </code></pre>
  
  <p>Even just knowing just which <code>UserID</code> has <code>Sales</code> > <code>fid</code> would be perfectly fine too:</p>
  
  <pre><code>UserId  COUNT(*)    COUNT(DISTINCT ud.SalesID)
    1        7               7
  </code></pre>
  
  <blockquote>
    <p><a href="http://sqlfiddle.com/#!9/1f8e66/1" rel="nofollow noreferrer">Online Example</a>.</p>
  </blockquote>
  
  <p>I tried several things such as <code>UNION</code>, but that just merged all the values together.</p>