简体   繁体   中英

Rearranging Mysql data in Alphabetical order

i have a marks table with studentid, marks, class_id, exam_id. i got marks and student_id from

$mark_query = $this->db->get_where('mark' , 
                                    array('class_id' => $class_id, 
                                           'exam_id' => $exam_id)
                );

After getting the marks and student_id i am displaying the marks with the student Name which i got it from another table with the student_id from the above query. I have a another table which have the student_id and their name.

The output is like this

Sajiv 25
Arun 35
Sonu 32
Binu 45

But i want to display the names in Ascending order.

Any ideas? I am using codeigniter.

Join the student table with mark table and order by student name in ascending order

    $this->db->select('students_table.name, mark.marks')
    ->from('mark')
    ->join('students_table', 'mark.student_id = students_table.id')
    ->where(['class_id' => $class_id, 'exam_id' => $exam_id])
    ->order_by('students_table.name', 'ASC')
    ->get();

Ordering results

$this->db->order_by()

Lets you set an ORDER BY clause.

The first parameter contains the name of the column you would like to order by.

The second parameter lets you set the direction of the result. Options are ASC, DESC.

$this->db->order_by('title', 'DESC');

// Produces: ORDER BY title DESC You can also pass your own string in the first parameter:

$this->db->order_by('title DESC, name ASC');

// Produces: ORDER BY title DESC, name ASC Or multiple function calls can be made if you need multiple fields.

$this->db->order_by('title', 'DESC');

$this->db->order_by('name', 'ASC');

// Produces: ORDER BY title DESC, name ASC If you choose the RANDOM direction option, then the first parameters will be ignored, unless you specify a numeric seed value.

You can always check CodeIgniter Query Builder for help. Here is the link: https://www.codeigniter.com/userguide3/database/query_builder.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM