如何在php mysql中获取重复列的最后一行

时间:2022-09-25 15:47:25

I have a table as follow. I want to run a query to select the data from the table using php in which if date column is repeated then I want to take only last row of that date

我有一张桌子如下。我想运行一个查询来使用php从表中选择数据,其中如果重复日期列,那么我只想取该日期的最后一行

id     Date          start   end    publish
1   04-Nov-2015     1000    1300    0
4   04-Nov-2015     2100    3500    0
5   05-Nov-2015     1500    3000    0

like for the below table, When I run the query then the result should come:

对于下表,当我运行查询时,结果应该来:

4   04-Nov-2015     2100    3500    0
5   05-Nov-2015     1500    3000    0

When I run the query

当我运行查询

$select = mysql_query("select * from `entry` Group by `date`") or die(mysql_error());

Then It shows the first row of repeating table, What should I modify in the query that the result should show the last row of repeating colum

然后它显示重复表的第一行,我应该在查询中修改哪个结果应该显示重复列的最后一行

5 个解决方案

#1


2  

Select * from (Select * from entry order by date,id desc) x group by x.date

#2


0  

You can do this with this approach:

你可以用这种方法做到这一点:

$select = mysql_query("select * from `entry` Group by `date`" ORDER BY id DESC LIMIT 1) or die(mysql_error());

#3


0  

Try inner query, I'm not sure following will work exactly as I cant test that now, but for getting result you have to use inner query. Inner query help me to get expected result in my case.

尝试内部查询,我不确定以下将完全按照我现在无法测试的方式工作,但是为了获得结果,您必须使用内部查询。内部查询帮助我在我的案例中得到预期的结果。

SELECT *
FROM entry p
WHERE id =
  (SELECT max(id) FROM entry p2
   WHERE p2.id = p.id)
GROUP BY p.date
ORDER BY p.id DESC;

#4


0  

This query will work for you:

此查询适用于您:

create TABLE test (id INT PRIMARY KEY, tdate DATE, start INT);

SELECT t1.* FROM test as t1
LEFT JOIN test as t2
  ON (t1.tdate = t2.tdate AND t1.id < t2.id)
WHERE t2.id IS NULL;

#5


0  

Try this query :-

试试这个查询: -

select * from( select * from entry order by id DESC ) new Group by date

#1


2  

Select * from (Select * from entry order by date,id desc) x group by x.date

#2


0  

You can do this with this approach:

你可以用这种方法做到这一点:

$select = mysql_query("select * from `entry` Group by `date`" ORDER BY id DESC LIMIT 1) or die(mysql_error());

#3


0  

Try inner query, I'm not sure following will work exactly as I cant test that now, but for getting result you have to use inner query. Inner query help me to get expected result in my case.

尝试内部查询,我不确定以下将完全按照我现在无法测试的方式工作,但是为了获得结果,您必须使用内部查询。内部查询帮助我在我的案例中得到预期的结果。

SELECT *
FROM entry p
WHERE id =
  (SELECT max(id) FROM entry p2
   WHERE p2.id = p.id)
GROUP BY p.date
ORDER BY p.id DESC;

#4


0  

This query will work for you:

此查询适用于您:

create TABLE test (id INT PRIMARY KEY, tdate DATE, start INT);

SELECT t1.* FROM test as t1
LEFT JOIN test as t2
  ON (t1.tdate = t2.tdate AND t1.id < t2.id)
WHERE t2.id IS NULL;

#5


0  

Try this query :-

试试这个查询: -

select * from( select * from entry order by id DESC ) new Group by date