E
E
EVOSandru62015-08-25 07:03:45
MySQL
EVOSandru6, 2015-08-25 07:03:45

How to sort by simple and complex parameter in mysql?

Good afternoon,
There are 3 tables: Users, Offers, Ratings. It is necessary that at first there was a sorting by ratings in descending order, then by the offered prices from the offer in ascending order. To have higher priority offers first:
Users:
--------------------
m_users
--------------------
id
name

--------------------
Offers:
--------------------
mс_offers
----- ---------------
id
user_id
price

--------------------
Ratings:
---------- ----------
ms_offers
--------------------
id
user_id
rate

---------------- ----
Approximately threw nonsense:

SELECT * FROM (SELECT * FROM m_users LEFT JOIN mc_rating ON m_users .id = mc_rating.user_id ORDER BY ( SUM(rate) / COUNT(rate) GROUP BY mc_rating.user_id ) as users  LEFT JOIN mc_offersON  users.id = mc_offers.user_id ORDER BY mc_offers.price

Help bring the request to mind.
As I understand it, in order to shove a good version of the query into AR , then this can only be done using findBySql ?!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Talgat Baltasov, 2015-08-25
@talgatbaltasov

SELECT * FROM m_users 
LEFT JOIN mc_rating ON m_users .id = mc_rating.user_id 
LEFT JOIN mc_offers ON  users.id = mc_offers.user_id
ORDER BY ( SUM(rate) / COUNT(rate) GROUP BY mc_rating.user_id ) DESC, mc_offers.price

Try this or I misunderstood you

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question