Search code examples
mysqlsqlmariadbadminer

MariaDB select only results greater than


I have a query that looks like the following:

select uid,leadcount,salescount,leadcount/salescount as diff from 
(
SELECT x.uid, COUNT(*) leadcount, COUNT(DISTINCT x.fid)
FROM total_leads AS x
WHERE x.uid BETWEEN 1 AND 5
GROUP BY x.uid
) t1 left join 
(
SELECT ud.UserId, COUNT(*) salescount, COUNT(DISTINCT ud.SalesID)
FROM total_sales AS ud
WHERE ud.UserId BETWEEN 1 AND 5
GROUP BY ud.UserID
) t2 on t1.uid=t2.UserId

It results in:

enter image description here

I'm trying to display only results where diff is greater than 2.5 (so in this case only uid 5 should be visible on output).

I tried using WHERE diff >= 2.5 but I got "You have an error in your SQL syntax".

Working example of MySQL


Solution

  • In MySQL and MariaDB, you can just add a HAVING clause to the end of the query:

    HAVING diff >= 2.5
    

    This acts like a WHERE but can use the alias.