Obtaining the ids preceding and proceeding the queried id - php

I have a feature that gets the record for specific id and has two links for the previous and next record. Right now I have separate queries to obtain the next id:
$query = "SELECT id FROM presentations WHERE id > '$getId' ORDER BY id ASC LIMIT 1";
$getId is the current id.
Is there a way to consolidate into one query?

SELECT A.id AS previd, B.id AS nextid
FROM
(SELECT id FROM presentations WHERE id < '$getId' ORDER BY id DESC LIMIT 1) A,
(SELECT id FROM presentations WHERE id > '$getId' ORDER BY id ASC LIMIT 1) B
Returns 2 columns: previd and nextid surrounding $getId

i think you can use inner query
SELECT id FROM presentations
WHERE id >= ( SELECT id FROM presentations WHERE id < '$getId' ORDER BY id DESC LIMIT 0 , 1 )
ORDER BY id ASC LIMIT 0 , 3
first array result will be previous record, second will be your current record, and the last will be your last record.

Related

mySQL connect 3 tables with no relation and take last data from them

I have three tables in mySQL database and I want to get only 1 LAST ENTERED DATA from all three tables (last entered data from table1, last entered data from table2, last entered data from table3). I was able to get first entered data of all tables this way:
SELECT dnevna.temp AS temp1, dnevna.hum AS hum1, podrum.temp AS temp2, podrum.hum AS hum2, spremnik_status.spremnik AS status_spremnik
FROM dnevna, podrum,spremnik_status
LIMIT 1
so then I tried for last entered data this way:
SELECT dnevna.temp AS temp1, dnevna.hum AS hum1, podrum.temp AS temp2, podrum.hum AS hum2, spremnik_status.spremnik AS status_spremnik
FROM dnevna, podrum, spremnik_stauts
ORDER BY dnevna.id, podrum.id,spremnik_stauts.id DESC
LIMIT 1
but I got an error and this is not a good way of doing it..soo how to do it?.
There is no relation between the tables because tables are updated randomly by 2 MCU-s that upload data in different time so the time, id autoincrement etc. can't match. Also, I have in 2 of 3 tables same column names (temp, hum are the same column names in 2 tables..so is that a problem? Do I need to rename?)
I need that sql select to make ONE php JSON string from 3 tables so I can encode it later for Android studio, MCU-u reading etc.
You can retrieve the last record from each table and cross-join those three queries:
SELECT *
FROM (SELECT temp AS temp1, hum AS hum1
FROM dnevna
ORDER BY id DESC
LIMIT 1) a
CROSS JOIN (SELECT temp AS temp2, hum AS hum2
FROM podrum
ORDER BY id DESC
LIMIT 1) b
CROSS JOIN (SELECT spremnik AS status_spremnik
FROM spremnik_status
ORDER BY id DESC
LIMIT 1) c
SELECT tb1.temp as temp1, tb1.hum as hum1,
tb2.temp AS temp2, tb2.hum AS hum2,
tb3.spremnik AS status_spremnik
FROM
(SELECT * FROM dnevna ORDER BY id DESC LIMIT 1) AS tb1,
(SELECT * FROM podrum ORDER BY id DESC LIMIT 1) AS tb2,
(SELECT * FROM spremnik_status ORDER BY id DESC LIMIT 1) AS tb3

mysql select last 5 records from first 50 records in the table

I want to select last 5 records from first 50 records in the table, currently i have following query, somebody tell me best way to select these records without calculating the limit and offset?
SELECT id FROM table WHERE enabled=1 ORDER BY date LIMIT 5, 45
Try this
SELECT id FROM (SELECT id FROM (SELECT id FROM table ORDER BY id ASC LIMIT 50) AS tbl ORDER BY id DESC LIMIT 5) as tbldata ORDER BY id ASC
this works:
SELECT id FROM (SELECT id,date FROM table ORDER BY date LIMIT 50) AS
temptable ORDER BY date DESC LIMIT 5

Selecting the available previous row not working mysql php

Well, i found this simple script online somewhere for selecting the next row and selecting the previous row.
This works. (Next page)
$currentid = $_GET['id'];
$nextquery= mysqli_query($conDB,"SELECT * FROM vids WHERE ID > $currentid ORDER BY ID ASC LIMIT 1")or die (mysqli_error($conDB));
However, this dosent, it returns the current page id instead of the previous id.
What i want is that it gets the last available id that is on the database.
$prevquery= mysqli_query($conDB,"SELECT * FROM vids WHERE ID < $currentid ORDER BY ID desc LIMIT 1")or die (mysqli_error($conDB));
Please someone help me, i would greatly appreciate it! :D
Best regards Dániel
To find the largest value of the field "ID" from the "vids" table, you will want to use the following query:
SELECT ID FROM vids ORDER BY ID DESC LIMIT 1
To find the previous and next rows in the table, the queries you are using look correct, however there appears to be a typo in the first line calculating the $currentid:
$currrentid = $_GET=['id'];
This line should be:
$currrentid = $_GET['id'];
remove DESC for the previous query
"What i want is that it gets the last available id that is on the database."
`SELECT MAX(ID) FROM table_name;`
will give you the last record id in a table.
Next:
select * from table_name where ID = (select min(ID) from table_name where ID > $_GET['id'])
Previous:
select * from table_name where ID = (select max(ID) from table_name where ID < $_GET['id'])
Or you can optimise the query and have it all completed in one call:
select * from table_name where (
ID = IFNULL((select min(ID) from table_name where ID > $_GET['id']),0)
or ID = IFNULL((select max(ID) from table_name where ID < $_GET['id']),0)
)

Select attribute of table based on min value from another table

My table structure is
I want to get the recent video uploaded of the user
What I want to do is:
"select video_id of a record who is having minimum of timestamp where userid='something'"
What I currently have is:
$recent_video = "$db->query("
SELECT video_id
FROM video_primary
ORDER BY timestamp DESC LIMIT 1
WHERE userid = '$userid'"
) or die($db->error);"`
while($row=mysqli_fetch_assoc($recent_video))
{
$video_id=$row['video_id'];
}
echo $video_id;
My table data is
How do I get the most recent view uploaded by the user?
Write WHERE clause after FROM part
Try this:
SELECT vp.video_id
FROM video_primary vp
WHERE userid='$userid'
ORDER BY vp.timestamp DESC
LIMIT 1;
try this
$recent_video=$db->query("select video_id from video_primary where userid='$userid'
ORDER BY timestamp DESC LIMIT 1 ") or die($db->error);
instead of
$recent_video="$db->query("select video_id from video_primary ORDER BY timestamp DESC
LIMIT 1 where userid='$userid'") or die($db->error);"
you are using where clause after order by and limit 1 which is a syntax error. see tutorial
Tutorial
There is a specific sequence of every clause in Query statement. Below is the sequence:
Select
Where
Order by
Limit
So, as per the above sequence, you should correct your query like below. Also, you should place double quote(") correctly.
$recent_video = $db->query("
SELECT video_id
FROM video_primary
WHERE userid = '$userid'
ORDER BY timestamp DESC LIMIT 1"
) or die($db->error);

Select on Mysql inverse order

i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order

Categories