请问如何写这样的SQL语句

leetow2006 2020-06-04 05:00:26
我用的是mySql,我有个入库表filmin,
create table if not exists filmin(name varchar(50) not null,type char(15) not null,
customer char(30) not null,size varchar(15),number smallint,inDate Date,memo varchar(50)) engine=InnoDB;
现在我想查询:根据字段name,type,customer组合唯一,并且入库日期inDate是最新的,
$query="select name,type,customer,number,size,inDate from filmin where name like '".$uname_p."%' and type like '".$utype_p."%' and customer like '".$ucust_p."%' group by customer,name,type order by inDate desc";
可是获取的记录,入库日期是最早的,不是最新的,请问:我的SQL语句应该怎么写?
其中$uname_p,$utype_p,$ucust_p是输入的变量值

...全文
186 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
带我飞的云 2020-06-10
  • 打赏
  • 举报
回复
引用 楼主 leetow2006 的回复:
我用的是mySql,我有个入库表filmin,
create table if not exists filmin(name varchar(50) not null,type char(15) not null,
customer char(30) not null,size varchar(15),number smallint,inDate Date,memo varchar(50)) engine=InnoDB;
现在我想查询:根据字段name,type,customer组合唯一,并且入库日期inDate是最新的,
$query="select name,type,customer,number,size,inDate from filmin where name like '".$uname_p."%' and type like '".$utype_p."%' and customer like '".$ucust_p."%' group by customer,name,type order by inDate desc";
可是获取的记录,入库日期是最早的,不是最新的,请问:我的SQL语句应该怎么写?
其中$uname_p,$utype_p,$ucust_p是输入的变量值


你这是按customer, name, type 分组后按时间排序了,所以取得的不一定是最新的日期
乐大师 2020-06-08
  • 打赏
  • 举报
回复
select name,type,customer,number,MAX(inDate) from filmin where name like '".$uname_p."%' and type like '".$utype_p."%' and customer like '".$ucust_p."%' group by customer,name,type
三年丿 2020-06-08
  • 打赏
  • 举报
回复
select name,type,customer,number,size,MAX(inDate)
from filmin A
where name like '".$uname_p."%' and type like '".$utype_p."%' and customer like '".$ucust_p."%' 
group by customer,name,type 
order by MAX(inDate) desc;
leetow2006 2020-06-08
  • 打赏
  • 举报
回复
怎么了,很难吗

56,940

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧