sql - Ordering selected data -


i'm making library project. want display persons have or haven't returned books in order: persons haven't returned books 15 days already, persons haven't returned books still have time until 15 days time limit , persons have returned books. table looks this:

id | pupil  | professor | date_of_taking      | expected_date       | returned | book | --------------------------------------------------------------------------------------- 1  |1       |1          | 2016-01-16 00:00:00 | 2016-01-30 00:00:00 | 0        | 20   | 2  |2       |1          | 2016-01-07 00:00:00 | 2016-01-14 00:00:00 | 0        | 93   | 3  |1       |1          | 2016-01-09 00:00:00 | 2016-01-30 00:00:00 | 1        | 14   | 

the pupil column contains id of person took book. professor column contains id of person gave book. book column contains id of taken book. of data in tables. date of taking date when person took book , expected date date until when person supposed return back.

so want if didn't understand previous explanation this: first show id 2 because time limit expired , supposed return book. after that, show 1 because still hasn't returned book has more time left. , finally, show 3 because has returned book. imagine there thousands of rows in table. how order show data in order explained here.

here have. need ordering data.

select      a.date_of_taking,     a.expected_at,     a.returned,     b.name pupil,     c.name prof,     d.author,     d.title taken inner join pupils b on a.pupil = b.id inner join users c on a.professor = c.id inner join books d on a.book = d.id limit 50 

and way, difference between 2 dates in table isn't strictly 15 day since table testing.
code in php want merge these 3 queries one. if it's possible.

$stmt = $this->db()->query("select          a.date_of_taking,         a.expected_at,         a.returned,         b.name pupil,         c.name prof,         d.autor,         d.title          taken         inner join pupils b on a.pupil = b.id         inner join users c on a.professor = c.id         inner join books d on a.book = d.id         a.returned='0' , a.date_of_taking < a.expected_at order a.date_of_taking asc");  $result = $stmt->fetchall(pdo::fetch_assoc); $stmt = $this->db()->query("select          a.date_of_taking,         a.expected_at,         a.returned,         b.name pupil,         c.name prof,         d.autor,         d.title          taken         inner join pupils b on a.pupil = b.id         inner join users c on a.professor = c.id         inner join books d on a.book = d.id         a.date_of_taking > a.expected_at order a.date_of_taking asc");  $var = $stmt->fetchall(pdo::fetch_assoc); foreach ($var $value) {     $result[] = $value; } $stmt = $this->db()->query("select          a.date_of_taking,         a.expected_at,         a.returned,         b.name pupil,         c.name prof,         d.autor,         d.title          taken         inner join pupils b on a.pupil = b.id         inner join users c on a.professor = c.id         inner join books d on a.book = d.id         a.returned='1' order a.date_of_taking asc");  $var = $stmt->fetchall(pdo::fetch_assoc); foreach ($var $value) {     $result[] = $value; } return $result; 

if specify

order returned, expected_date 

then returned = 0 come before returned = 1 , within order expected_date more in future order within that.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -