N
N
Nikolaymac2016-08-23 08:05:19
MySQL
Nikolaymac, 2016-08-23 08:05:19

Yii how to make a complex database query?

The essence is as follows
There is a Camps table
-id_camp
-address
-visits_limits
and a Visits table -id -time_v
-id_camp And so you need to select the desired row from the Camps table by address but the limit of visits is more than visits in the visits table I made a normal query almost) does not work only in happens if the visits table is empty at all Tell me how to make a request in the usual form and, most importantly, in the yii 1 syntax! SELECT * FROM `camps` LEFT JOIN visits on camps.id_camp = visits.id_camp WHERE address = '$address' AND visit_limit > (SELECT count(id) FROM visits WHERE visits.id_camp = camps.id_camp GROUP BY visits.id_camp) GROUP BY camps.id_camp

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
S3Ga, 2016-08-25
@S3Ga

SELECT * FROM `camps` LEFT JOIN visits on camps.id_camp = visits.id_camp WHERE address = '$address' AND visit_limit > COALESCE((SELECT count(id) FROM visits WHERE visits.id_camp = camps.id_camp GROUP BY visits.id_camp),0) GROUP BY camps.id_camp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question