CodeIgniter Forums
help me to change mysql code to php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: help me to change mysql code to php (/showthread.php?tid=68188)



help me to change mysql code to php - kira - 06-07-2017

can anyone help me changing this code to php?

SELECT a1.mark_id, a1.student_id,  
COUNT(a2.quiz) Rank 
FROM mark a1, mark a2 WHERE a1.quiz < a2.mark_quiz
OR (a1.mark_total=a2.quiz AND a1.student_id = a2.student_id) 
GROUP BY a1.student_id, a1.quiz ORDER BY a1.quiz DESC, a1.student_id DESC


thanks.


RE: help me to change mysql code to php - skunkbad - 06-07-2017

There is no reason why you can't run this query as is:


PHP Code:
// In your model
$this->load->database();

$query $this->db->query('
     SELECT a1.mark_id, a1.student_id,  
     COUNT(a2.quiz) Rank 
     FROM mark a1, mark a2 WHERE a1.quiz < a2.mark_quiz
     OR (a1.mark_total=a2.quiz AND a1.student_id = a2.student_id) 
     GROUP BY a1.student_id, a1.quiz ORDER BY a1.quiz DESC, a1.student_id DESC
'
);

if( 
$query->num_rows() > )
{
     // Do something with $result ...
     $result $query->result();




RE: help me to change mysql code to php - ivantcholakov - 06-07-2017

a1.mark_id, a1.student_id, COUNT(a2.quiz) AS Rank

You want to show two simple fields and one aggregated field. Probably the newer MySQL servers would refuse to execute such a query.


RE: help me to change mysql code to php - Paradinight - 06-07-2017

(06-07-2017, 05:19 PM)ivantcholakov Wrote: a1.mark_id, a1.student_id, COUNT(a2.quiz) AS Rank

You want to show two simple fields and one aggregated field. Probably the newer MySQL servers would refuse to execute such a query.

the sql is valid. it will work.


RE: help me to change mysql code to php - ivantcholakov - 06-07-2017

I don't see a1.mark_id participating in GROUP BY clause.


RE: help me to change mysql code to php - Paradinight - 06-08-2017

(06-07-2017, 09:31 PM)ivantcholakov Wrote: I don't see a1.mark_id participating in GROUP BY clause.

The sql syntax will work. I would not do that Smile