mysql - Adding columns (from other tables) to the 'most recent notes for each user' query sql -
i have following tables: [run code snippet]
<!--__________________________________________________--> ---------------------'notes' table----------------------- <!--__________________________________________________--> <table> <thead> <th>note_id</th> <th>note_date</th> <th>note_time</th> <th>writer_id</th> </thead> <tbody> <tr> <td>1</td> <td>2-05-2016</td> <td>08:37:05</td> <td>1</td> </tr> <tr> <td>2</td> <td>14-05-2016</td> <td>11:44:01</td> <td>2</td> </tr> <tr> <td>3</td> <td>14-05-2016</td> <td>07:57:35</td> <td>2</td> </tr> <tr> <td>4</td> <td>26-05-2016</td> <td>15:02:22</td> <td>3</td> </tr> <tr> <td>5</td> <td>27-05-2016</td> <td>11:39:04</td> <td>4</td> </tr> </tbody> </table> <!--__________________________________________________--> ---------------------'writers' table----------------------- <!--__________________________________________________--> <table> <thead> <th>writer_id</th> <th>first name</th> <th>last name</th> </thead> <tbody> <tr> <td>1</td> <td>john</td> <td>doe</td> </tr> <tr> <td>2</td> <td>jane</td> <td>doe</td> </tr> <tr> <td>3</td> <td>jame</td> <td>doe</td> </tr> <tr> <td>4</td> <td>lame</td> <td>doe</td> </tr> <tr> <td>5</td> <td>shame</td> <td>doe</td> </tr> </tbody> </table>
i want display newest notes in following format:
<table> <thead> <th>first name</th> <th>last name</th> <th>note_date</th> <th>note_time</th> </thead> <tbody> <tr> <td>john</td> <td>doe</td> <td>2-05-2016</td> <td>08:37:05</td> <td>1</td> </tr> <tr> <td>jane</td> <td>doe</td> <td>14-05-2016</td> <td>11:44:01</td> <td>2</td> </tr> <tr> <td>jame</td> <td>doe</td> <td>26-05-2016</td> <td>15:02:22</td> <td>3</td> </tr> <tr> <td>lame</td> <td>doe</td> <td>27-05-2016</td> <td>11:39:04</td> <td>4</td> </tr> </tbody> </table>
i have following sql query correctly returns latest notes (although getting both notes jane doe, writer id 2 due notes on same date):
note: not concerned duplicate now.
select a.note_id, a.note_date, a.note_time notes inner join ( select note_id, max(note_date) note_date notes group note_id ) b on a.note_id = b.note_id , a.note_date = b.note_date
i want add writers names query have no idea of how add join:
(notes inner join writers on notes.writer_id = writers.writer_id)
here 1 way add join
:
select w.name, n.* notes n inner join (select note_id, max(note_date) note_date notes group note_id ) nn on n.note_id = nn.note_id , n.note_date = nn.note_date inner join writers w on n.writer_id = w.writer_id;
Comments
Post a Comment