Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql get last 2 records of table with 3 tables

Tags:

php

mysql

I have 3 mysql tables (order , camp , user) as bellow values,

order_table
ID    camp_id       orderDate       message
1       1           2015-01-01      ok
2       1           2015-02-01      ok
3       2           2015-03-01      not ok
4       3           2015-01-01      not ok
5       1           2015-04-01      not ok
6       2           2015-01-01      ok
7       3           2015-07-01      not ok

camp_table
camp_id camp_uid     camp name
1       10             first camp
2       11             second camp
3       12             third camp
4       10             forth camp

user_table
uid    uname
10      abc
11      xyz
12      wrt

i want to have result as bellow

uname,camp name,message

for last 2 records of each user from order_table for today order by orderDate

I want to join these tables to have uname from user_table and camp name from camp_table and message from order_table. for today order by orderDate Thanks

like image 974
Mr.SH Avatar asked Sep 15 '16 13:09

Mr.SH


1 Answers

Select
ct.camp_name,
ut.uname,
ot.message FROM order_table as ot
LEFT JOIN camp_table as ct on ot.camp_id = ct.camp_id
LEFT JOIN user_table as ut on ct.camp_uid = ut.uid
order by ot.id desc
limit 2
like image 79
Punit Gajjar Avatar answered Oct 01 '22 05:10

Punit Gajjar