数到一个数 x - sql [英] Count up to a number x - sql

查看:48
本文介绍了数到一个数 x - sql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有 100 条记录的表,我必须数到 50,然后从 51 中删除其他 49 条

//开始会话session_start();//包括连接包括'connessione.php';//查询从数据库中获取东西$query_string = "SELECT * FROM utenti ORDER BY id DESC LIMIT 50";$query = mysqli_query($connessione, $query_string);//获取结果$results = mysqli_fetch_assoc($query);//把它变成一个JSON数组$results = json_encode( $results );//将这些结果放在一个文件中(如果文件不存在则创建)$fileName = 'backup_file_' .时间() .'.文本文件';$file = fopen( $fileName , 'a' );fwrite( $file, $results );fclose( $file );//删除刚才备份的行$query_delete = "从 utenti ORDER BY id DESC LIMIT 50 中删除";mysqli_query( $connessione, $query_delete );

?>所以我拿出最后的 50,但如果我必须数到 A 并从 B 开始消除?

解决方案

您可以更改

DELETE FROM utenti ORDER BY id DESC LIMIT 50

到这个查询

DELETE FROM utenti ORDER BY id ASC LIMIT 50

<块引用>

用户已询问如何解决其他问题,请告知

...你上面的代码$results = json_encode( $results );//将这些结果放在一个文件中(如果文件不存在则创建)$fileName = 'backup_file_' .时间() .'.文本文件';$file = fopen( $fileName , 'a' );fwrite( $file, $results );fclose( $file );//删除你刚刚备份的行,但前提是有50行或更多行.if( sizeof( json_decode( $results, true ) ) >= 50 ){$query_delete = "从 utenti ORDER BY id ASC LIMIT 50 中删除";mysqli_query( $connessione, $query_delete );}

I have a table with 100 records, I have to count up to 50 and from 51 to delete the other 49

//Start the session
session_start();

//Include connection
include 'connessione.php';

//Query to get stuff from database
$query_string = "SELECT * FROM utenti ORDER BY id DESC LIMIT 50";
$query = mysqli_query($connessione, $query_string);

//Get results
$results = mysqli_fetch_assoc($query);

//Make that into a JSON array
$results = json_encode( $results );

//Put those results in a file (create if file not exist)
$fileName = 'backup_file_' . time() . '.txt';
$file = fopen( $fileName , 'a'  );
fwrite( $file, $results );
fclose( $file );

//Delete the rows that you just backed up
$query_delete = "DELETE FROM utenti ORDER BY id DESC LIMIT 50";
mysqli_query( $connessione, $query_delete );

?> so I pull out the last 50 but if I had to count to A and eliminate from B onwards?

解决方案

You can change

DELETE FROM utenti ORDER BY id DESC LIMIT 50

to this query

DELETE FROM utenti ORDER BY id ASC LIMIT 50 

user has asked for how to fix other issue, inform

...your code above

$results = json_encode( $results );

//Put those results in a file (create if file not exist)
$fileName = 'backup_file_' . time() . '.txt';
$file = fopen( $fileName , 'a'  );
fwrite( $file, $results );
fclose( $file );

//Delete the rows that you just backed up, but only if there are 50 or more.
if( sizeof( json_decode( $results, true ) ) >= 50 ){
    $query_delete = "DELETE FROM utenti ORDER BY id ASC LIMIT 50";
    mysqli_query( $connessione, $query_delete );
}

这篇关于数到一个数 x - sql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆