简体   繁体   中英

selecting recently added data using joins in mysql

In this i am trying to display the user details including users payment details,
I have the table like userpayment and userdetails.

userpayment table consists of fields like paymentid,userid,paidamount,planid,planname,validmonths,validupto,createdat

userdetails table consists of userid,username,dob,address,gender,mobile,createdat .

In userdetails table, every entry is an unique one.But in userpayment table single user can have multiple payment details.I need to fetch the details of every users including users payment payment details,record of payment details must be unique one and it must be recently inserted one.

I am using codeigniter,

My query is,

$res=$this->db->query(select * from userdetails r,userpayment p where r.userid all (\"select userid from userpayment GROUP BY userid HAVING max(paymentid) \")");

Please correct it if there is any mistakes in my query and give solutions for this.

select * from userdetails where 
userid = (select userid from paymentdetails order by createdat desc limit 1)

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