防止重复行

发布于 2024-12-22 05:06:25 字数 1375 浏览 0 评论 0原文

我有这个表:

在此处输入图像描述

我想删除相同的行。例如,前五行是相同的,我的表应该只有一行包含此数据:40.792274 29.412994 2011-12-21 17:19:52。

所以我使用了以下代码:

$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){

    $date = $row['date'];
    $lat = $row['latitude'];
    $lon = $row['longitude'];
    $query = "SELECT * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon'";
    $re = mysql_query($query);
    $number = mysql_num_rows($re);
    $number--;

    $query = "DELETE * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon' LIMIT $number";

    mysql_query($query);
}

但是这个代码不起作用..我应该做什么?

已编辑

我解决了我的问题:

$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){

    $date = $row['date'];
    $lat = $row['latitude'];
    $lon = $row['longitude'];
    $query = "SELECT * FROM table WHERE date='$date' AND latitude=$lat AND    longitude=$lon";
    $re = mysql_query($query);
    $number = mysql_num_rows($re);
    $number--;

    $query = "DELETE FROM table WHERE date='$date' AND latitude=$lat AND longitude=$lon LIMIT $number";

    mysql_query($query);
}

第一个问题中的查询行不正确。

I have this table :

enter image description here

I would like to delete same rows. For example first five rows are the same, my table should have only one row that includes this data : 40.792274 29.412994 2011-12-21 17:19:52.

So I used the following code :

$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){

    $date = $row['date'];
    $lat = $row['latitude'];
    $lon = $row['longitude'];
    $query = "SELECT * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon'";
    $re = mysql_query($query);
    $number = mysql_num_rows($re);
    $number--;

    $query = "DELETE * FROM table WHERE date='$date' AND latitude='$lat' AND longitude='$lon' LIMIT $number";

    mysql_query($query);
}

But this code doesn't work.. What should I do ?

Edited :

I solved my question :

$query = "SELECT * FROM table GROUP BY date";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){

    $date = $row['date'];
    $lat = $row['latitude'];
    $lon = $row['longitude'];
    $query = "SELECT * FROM table WHERE date='$date' AND latitude=$lat AND    longitude=$lon";
    $re = mysql_query($query);
    $number = mysql_num_rows($re);
    $number--;

    $query = "DELETE FROM table WHERE date='$date' AND latitude=$lat AND longitude=$lon LIMIT $number";

    mysql_query($query);
}

Query lines were incorrect in my first question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红墙和绿瓦 2024-12-29 05:06:25

要删除重复的元素,您可以使用类似以下的内容:

$q = "SELECT date FROM table GROUP BY date"
$r = mysql_query($r);
$date = '';
while($row = mysql_fetch_array($r)){
    $date = $row['date'];
    $q = "SELECT date FROM mytable WHERE date='$date'";
    $re = mysql_query($q);
    $num = mysql_num_rows($re);
    $num--;
    $q = "DROP FROM mytable WHERE date='$date' LIMIT $num";
    mysql_query($q);
}

应该可以解决问题。更具体地说,当创建 $date 值时,您必须为 PHP 提供一个要使用的时间。 date() 默认使用当前时间,但您可以为其提供自定义时间作为第二个参数。

我建议您也查看 php.net 上的 strtotime() 手册(将数据库中的时间转换为可与 date() 一起使用的时间戳)。

编辑:上面的答案已被编辑以删除所有重复条目。

To remove the duplicate elements, you would use something like this:

$q = "SELECT date FROM table GROUP BY date"
$r = mysql_query($r);
$date = '';
while($row = mysql_fetch_array($r)){
    $date = $row['date'];
    $q = "SELECT date FROM mytable WHERE date='$date'";
    $re = mysql_query($q);
    $num = mysql_num_rows($re);
    $num--;
    $q = "DROP FROM mytable WHERE date='$date' LIMIT $num";
    mysql_query($q);
}

Should do the trick. More specifically, when creating your $date value, you have to provide PHP with a time to use. date() defaults to using the current time, but you can provide it with a custom time as the second argument.

I suggest you take a look at the strtotime() manual at php.net as well (To translate times in your db to timestamps that can be used with date() ).

EDIT: The Answer above has been edited to remove all duplicate entries.

怼怹恏 2024-12-29 05:06:25

尝试更改 $dateOfNewData = date('Ymd H:i:s');

$dateOfNewData = date('Y-m-d 00:00:00'); //or change the first 00 to H if you need it to match by hour, second 00 to i if you need to match minutes and the same with seconds.

$dateOfNewData = date('Ymd') 几乎相同并且适用于日期时间字段类型

并且您还需要将查询修改为类似这样的内容,除非您需要确切的时间:

< code>"SELECT * FROM mytable WHERE date = '$dateOfNewData'" // 如果您在数据库中处理过去的数据,您可能还需要结束日期。

Try changing $dateOfNewData = date('Y-m-d H:i:s');
to

$dateOfNewData = date('Y-m-d 00:00:00'); //or change the first 00 to H if you need it to match by hour, second 00 to i if you need to match minutes and the same with seconds.

or $dateOfNewData = date('Y-m-d') which is pretty much the same and works with datetime field types

And you also need to modify your query to something like this unless you need an exact time:

"SELECT * FROM mytable WHERE date = '$dateOfNewData'" // you might also want the end date if you're working with the past in your database.

爱人如己 2024-12-29 05:06:25

好吧,你可以像“Ignas”建议的那样尝试,但你也可以尝试这个:

首先获取日期(年,月,日),没有小时,分钟和秒。如果您使用完整日期格式,那么您需要匹配完全相同的时间。 (同样的)我猜这并不是你真正想要的。所以你可以使用这个:

$dateOfNewData = date('Y-m-d'); //just get year, month, day in right format (2011-12-20)

然后运行查询。在这里你有更多的选择,但我认为更简单的是这样的:

"SELECT * FROM mytable WHERE date_col LIKE '$dateOfNewData%' GROUP BY date_col" 

这会将相同的日期分组在一起,并且仅显示一次,并将匹配 'date_col 以示例:2011-12-20% 开头的所有行(这就是为什么我使用 LIKE 和 $dateOfNewData%)

$dateOfNewData 包含以下格式的当前日期:年-月-日 (2011-12-20) 并且在 Mysql 查询中不要忘记在末尾使用 % 日期。例如,它就像 Windows 中的 * 。

将“mytable”替换为您的表名称,将“date_col”替换为日期列。

Well you can try like "Ignas" suggest but you cal also try this:

First just get the date (year, month, day) without hour, minutes and seconds. If you use full date format then you need to match exactly the same time. (to second the same) which is not really what you are looking for i guess. So you can use this:

$dateOfNewData = date('Y-m-d'); //just get year, month, day in right format (2011-12-20)

Then run a query. Here you have more options but i think the easier is something like that:

"SELECT * FROM mytable WHERE date_col LIKE '$dateOfNewData%' GROUP BY date_col" 

This will group the same dates together and will display just once and will match all the rows where 'date_col starts with example: 2011-12-20% (thats why i use LIKE and $dateOfNewData%)

$dateOfNewData contains current date in this format:year-month-day (2011-12-20) and in Mysql query dont forget to use % at the end of the date. It's like * in windows for example.

'mytable' replace with your table name and 'date_col' with date column.

扭转时空 2024-12-29 05:06:25

您使用的 date() 将给出当前日期时间,因此请尝试使用 mktime() 来获取您想要的确切日期时间。

你必须稍微改变你的查询,我修改了下面的查询,

$query = mysql_query("SELECT * FROM mytable WHERE date='$dateOfNewData'");

在mysql中日期或日期时间应该在''内。

date() you have used will give current date time , so try to use mktime() to get extact date time you want.

you have to change your query little bit, I have modified query below,

$query = mysql_query("SELECT * FROM mytable WHERE date='$dateOfNewData'");

In mysql Date or datetime coulmn should be within ''.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文