D
D
DLISM2022-02-17 13:42:14
Yii
DLISM, 2022-02-17 13:42:14

How to sort a table by related fields?

I welcome everyone!
I have two tables Host (fields id, ...request) and Request (fields id, host_id...) I need to get records from the Host table sorted by the field request. What is the correct way to write a request?

return Host::find()
                ->select('*, (SELECT count(id) FROM request WHERE request.host_id=id) as test_feild')
                ->orderBy('test_feild DESC')
                ->limit(10)
                ->all();

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question