Let's make this simple:
I have a table called chat that has many rows in, every user can add a new message and this means the table can get quite large.
(I have looked online and it's not very 'clear' on some examples.)
I need to delete all older messages, keeping only say 25 of the newest ones. Now I wish to do it via id, as my id is set to auto-increment.
This is what I have but it doesn't work:
DELETE FROM `chat` ORDER BY `id` DESC LIMIT 0,50
I found it and changed it, but no luck!
I just not sure what to try. I am not very experienced, I know the basics, any help is very appreciated.
to keep 50 records:
DELETE FROM `chat`
WHERE id NOT IN (
SELECT id
FROM (
SELECT id
FROM `chat`
ORDER BY id DESC
LIMIT 50
) foo
);
I saw your php code in the comment and editing my answer which is working if you are looking for this... For deleting all old rows keeping 25 of the newest rows just first select all the record from your chat table, then count the same, estimate your limit i.e. deducting 25 from the count, thereafter, run a delete on the chat table but in DESC order and give the limit which was estimated by deducting 25 from the count.
$sqli = "SELECT * FROM chat";
$result = $conn->query($sqli);
$count = count($result);
$limit = $count - 25;
//echo $limit;
$sql = "DELETE FROM chat ORDER BY id DESC LIMIT $limit";
if ($conn->query($sql) === TRUE)
{
echo "true";
}
else
{
echo "false";
}
Related
I'm trying to get all the records from this ID to last record inserted.
example: 40 records and I want to display data from id(33) to id(40).
Something like this.
Select * from records between 33 and last_id;
My current SQL.
sql = "SELECT id, B5, B6, datetime FROM child_records ORDER BY id DESC LIMIT 10";
Use following
This query return all record where id greater than 32
Select * from records where id > 32;
You make the first $sql query to always get the last id in table
$last_id = always the last id in the table
Then in $row['id'] it is the last id you got from SQL query. You store it in variable then use it in main query
// query to get last id
$sql = "SELECT * FROM `child_records` order by `id` DESC LIMIT 1";
$result = mysqli_query($con, $query);
$row = mysqli_fetch_array($result);
$last_id = $row['id']; // now you got the last id in table even if you inserted new columns
// main query
$sql = "SELECT `id`, `B5`, `B6`, `datetime`
FROM `child_records`
WHERE `id` BETWEEN 33 AND {$last_id} ";
Then you know how to complete the query right ? just like the query to get last id
hope that answer your question
I have a dynamic MySQL-Table with 20 columns and 1 Row where id = 1. The new data will be added to the table as the sencond row where id = 2, And so an. I need to read the newest data row (always the last id). How should I write the query to read the last row?
How should I change this:
$sql = "SELECT id, AbW_L, PuVor_L ,Durchfluss ,... FROM table order by id desc ";
Order by id descending, and limit the results to one row:
SELECT * from table order by id desc limit 1;
You might want to take a look at: http://www.techonthenet.com/sql/order_by.php
It will give you more detailed information on what to do, however based on that one line of code, just change it to:
$sql = "SELECT * FROM `table_name` ORDER BY `id` DESC";
Should work no problem. If you want to add limits, you can do:
$sql = "SELECT * FROM `table_name` ORDER BY `id` DESC LIMIT 10";
I selected 10 as the limit in the example, you can pick whatever number you would like to use.
I just used this query
SELECT * FROM questions
ORDER BY RAND()
LIMIT 20;
On mysql database I have a column called display (along with the columns of questions) where the values are equal to 1.So now as you can see in the query I have limited to 20.I want to change the value of the all the 20 to display=0.I know this is the query
update test3
set display=0
where id=11;
But this just sets the display of one row.How do I do it for the 20.
Thanks in advance.
you can do this
update test3
set display=0
where id in (select id from questions order by rand() limit 20);
Suppose you are using php.
$result = msyql_query("select id from questions order by rand() limit 20");
$ids = array();
while($row = msyql_fetch_assoc($result)){
$ids[] = $row['id'];
}
For your condition, first perform the first query and save the ids from the first query.
Suppose you are using php and you have saved the ids from the first query in $ids;
you can run the second query like this;
$idstring = implode(',',$ids);
$query = 'update test3 set display=0 where id in ('.$idstring.' )';
$result = mysql_query($query);
Use the WHERE clause to search a subquery
UPDATE test3
SET display = 0
WHERE id IN (
SELECT id FROM questions
ORDER BY RAND()
LIMIT 20)
If you want to perform something between the SELECT and the UPDATE try this:
CREATE TABLE #Temp (
division TINYINT
)
INSERT INTO #Temp
SELECT id FROM questions
ORDER BY RAND()
LIMIT 20
--SELECT * FROM #Temp
UPDATE test3
SET display = 0
WHERE id IN (
SELECT division FROM #Temp)
in database, the table friendship has id, username and friendusername. When a user accept other user as friend, 1 row of record will be saved in the table (not 2 rows). I want to random select 15 users and display their pictures, mysql code :
<?php
$query120 = "SELECT frenusername FROM friendship WHERE username='{$username2}' UNION SELECT username FROM friendship WHERE friendusername='{$username2}' AND RAND()<(SELECT ((15/COUNT(*))*10) FROM friendship) ORDER BY RAND() LIMIT 15";
$result120 = mysql_query($query120,$connection) or die (mysql_error());
confirm_query($result120);
while($userinfo120 = mysql_fetch_array($result120)){
$frenusername= $userinfo120['frenusername'];
$username = $userinfo120['username']; //this hold empty value, why?
$query121 = "SELECT picturemedium FROM users WHERE username='{$frenusername} OR username='{$username}' LIMIT 1";
//display picture
}
}
?>
My problem 1 :
Why $username hold empty value?
My problem 2 :
The statement AND RAND()<(SELECT ((15/COUNT(*))*10) FROM friendship) ORDER BY RAND() LIMIT 15" is to random select 15 records from the table. Supposedly I need to write it on both UNION statements right? (meaning I need to write it for 2 times). If so, it will show 30 records instead of 15 records. Should I change it become 8/Count LIMIT 8 for each? Or is there any other way avoid duplicate the AND statement?
If you just need a random set of 15 friends of a username2, then you don't need the UNION:
(SELECT username
FROM friendship
WHERE (friendusername='{$username2}' OR
username='{$username2}') AND
RAND()<(SELECT ((15/COUNT(*))*10) FROM friendship)
ORDER BY RAND()
LIMIT 15)
The UNION will select everything from either SELECT. In your SQL, the first SELECT would get ALL of the usernames in the friendship table, then the second SELECT would get the random 15, but the UNION would select from either of the two SELECTs a record if it was in either of the two SELECT results. That's probably why you're seeing the extra users.
I am displaying a list.
The user can edit any item.
I am displaying next previous buttons which when clicked it dynamically fetches the next previous records form db. no page reload.
this is working well with id. like how can i get next and previous field id with php from mysql and MySQL next/previous ids (arbitrary sort order)
Currently i am displaying like
select * from table order by name asc
and for next and previous i used like this...
select * from table where id > $id order by name asc limit 1
select * from table where id < $id order by name desc limit 1
the above is what i got when i searched the internet.
in this order the id will not be in an order so i cannot use id to fetch next and previous record i think so.
i want to get next and previous records with respect to any other field and not by id.
so some thing like this.
select * from table where id > 5 order
by name asc or desc limit 1.
what would be the query?
Note
There are 15000 records. which is half filled by default. the rest half is supposed to be updated by admin. so instead of reloading the page after the admin had updated one record i fetch the other and show dynamically on the screen so that he can continue updating content. there will not any page reload. he just clicks update and the next record appears. he can select previous or next button to navigate between records.
You need to filter on the value of name:
SELECT * FROM `table`
WHERE name < 'Foo'
ORDER by name DESC
LIMIT 1
This only works if the names are unique. If there can be duplicates you need a tie-breaker. You can for example use the id column to tie-break.
SELECT * FROM `table`
WHERE name < 'Foo' OR (name = 'Foo' AND id < 42)
ORDER by name DESC, id DESC
LIMIT 1
You're better doing more tradition pagination.
$itemsPerPage = 10;
$page = (int)$_GET['page']; // do more validation to check this is a valid integer
$start = ($page * $itemsPerPage) - $itemsPerPage;
$sql = "SELECT * FROM `table`WHERE name < ? ORDER by name DESC limit $start, $itemsPerPage";