Skip to content
Advertisement

Mysql – how to sort results by values in columns?

I have two database tables customers which contains data about customers with the scheme like that:

and customers_facts which keeps the customers facts in a form of fact_name and corresponding fact_value.

I want to create a query to get all customer_id where name=Milton and city=Milan sorted by birthday and company. So in my example the results would be:

I have a query which gets all the customers_id where name=Milton and city=Milan

But I have no idea on how to sort the results by fact_value How to do it ? Is it even possible with such scheme ?

Advertisement

Answer

This is a little tricky. You can’t filter easily before aggregating. So, do the filtering in the having clause:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement