简体   繁体   English

使用mysql中的连接选择最近添加的数据

[英]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和userdetails这样的表。

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

userdetails table consists of userid,username,dob,address,gender,mobile,createdat . userdetails表由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. 在userdetails表中,每个条目都是唯一的。但是在userpayment表中,单个用户可以有多个付款细节。我需要获取每个用户的详细信息,包括用户支付付款详细信息,付款详细信息必须是唯一的,必须是最近插了一个。

I am using codeigniter, 我正在使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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