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.
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
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";
}
I'm using this query to select a set of records from a MySQL database:
SELECT *
FROM table
WHERE id IN(10,14,12,11,8,7,4)
AND actief='on'
ORDER BY FIELD(id,10,14,12,11,8,7,4)
This will give me the given ID's. In this case I will get a maximum of 7 records. But it can be less if for example ID '14' has active='off'.
But what I need is a set of 20 records where het list IN(10,14,12,11,8,7,4) must be in the result if they also meet the condition active='on'. If this returns 6 records, then I want the query to select another 14 records. Selecting highest ID first, must meet active='on' and may not already be in the result.
Can this be achieved by one SQL statement. Or should I first put the result of the mentionend query in an array and in a second array select the remaining records. And finaly put those also in the array?
You want to sort rather than filter the results. I think this is the query you want:
SELECT *
FROM table
ORDER BY (id IN(10,14,12,11,8,7,4) AND actief = 'on') desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;
EDIT:
The final solution only wanted actief = 'on', so:
SELECT *
FROM table
WHERE actief = 'on'
ORDER BY (id IN (10,14,12,11,8,7,4)) desc,
FIELD(id,10,14,12,11,8,7,4),
id desc
LIMIT 20;
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)
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";