获取特定约束的最后插入值

时间:2016-03-14 10:55:45

标签: php mysql

我需要获取m_id = 2的最后插入值。我有两条m_id = 2的记录,其中Id为主键我需要max(id) m_id=2

例如

id m_id MName

2 2样本

3 2 sample1

我只需要获取Id=3的记录。我需要检查以下查询的MAX(id)

select a.*,b.MName,b.ZipCode,b.City,b.Address,b.email from insurance_details as a,insmemberdetails as b where b.id='2' and a.m_id='2'

3 个答案:

答案 0 :(得分:0)

使用ORDER BY ID DESC和LIMIT 1:

select a.*,b.MName,b.ZipCode,b.City,b.Address,b.email 
from insurance_details as a,insmemberdetails as b 
where b.id='2' and a.m_id='2'
order by b.id desc 
limit 1

答案 1 :(得分:0)

您可以像这样使用它:

select a.*,b.MName,b.ZipCode,b.City,b.Address,b.email 
from insurance_details as a,insmemberdetails as b 
where a.m_id='2' order by b.id desc limit 1

你也可以这样使用它:

select a.*,b.MName,b.ZipCode,b.City,b.Address,b.email 
from insurance_details as a,insmemberdetails as b 
where b.id= (SELECT MAX(id) FROM b) and a.m_id='2'

答案 2 :(得分:0)

尝试此查询: -

select a.*,b.MName,b.ZipCode,b.City,b.Address,b.email 
from insurance_details as a,insmemberdetails as b 
where b.id='2' and 
m_id = (SELECT MAX(m_id) FROM insurance_details)

希望它会对你有所帮助: - )