I need to update the second-to-last row in a table given a set of conditions. If it was a SELECT query it would look like this:
SELECT col_1 FROM table_1 WHERE id = id_# ORDER BY timestamp_col DESC LIMIT 1,1
However, I get an error when I do the following but it's the behavior I want:
UPDATE table_1 SET col_1 = value_# ORDER BY timestamp_col DESC LIMIT 1,1
I'm aware I can write this
UPDATE table_1 SET col_1 = value_# ORDER BY timestamp_col DESC LIMIT 1
But I can't figure out how to get the second-to-last row to update given the set of conditions.
What is the correct query to update the second-to-last row?
MySQL does not support the "offset" feature in the LIMIT clause for UPDATE or DELETE operations.
But it is supported for SELECT statements.
So what you can do to get around this is wrap the offset LIMIT selection in a joined subselect, then update only the row where the id and timestamp_col equals the joined id and timestamp_col like so:
UPDATE table_1 a
INNER JOIN
(
SELECT id, timestamp_col
FROM table_1
WHERE id = <id>
ORDER BY timestamp_col DESC
LIMIT 1,1
) b ON a.id = b.id AND a.timestamp_col = b.timestamp_col
SET a.col_1 = <value>
Related
I am trying get the latest 10 rows and delete remaining rows of a particular id with one query.
Here is my query:
DELETE
FROM `courses`
WHERE customer_id = 11
ORDER BY id ASC LIMIT (SELECT COUNT(*) FROM `courses` WHERE customer_id = 11);
Please anyone help me to recognize the error in this query.
Thanks.
i found the solution, here is the query :
$sql1="DELETE FROM `courses` WHERE customer_id =11 AND id NOT IN (SELECT id
FROM (SELECT id FROM `courses` WHERE customer_id =11 ORDER BY id DESC LIMIT 10) AS foo)";
$conn->query($sql1);
By above query, we get the latest 10 rows and delete the remaining rows of a particular id.
I have a table as follow. I want to run a query to select the data from the table using php in which if date column is repeated then I want to take only last row of that date
id Date start end publish
1 04-Nov-2015 1000 1300 0
4 04-Nov-2015 2100 3500 0
5 05-Nov-2015 1500 3000 0
like for the below table, When I run the query then the result should come:
4 04-Nov-2015 2100 3500 0
5 05-Nov-2015 1500 3000 0
When I run the query
$select = mysql_query("select * from `entry` Group by `date`") or die(mysql_error());
Then It shows the first row of repeating table, What should I modify in the query that the result should show the last row of repeating colum
Select * from (Select * from entry order by date,id desc) x group by x.date
You can do this with this approach:
$select = mysql_query("select * from `entry` Group by `date`" ORDER BY id DESC LIMIT 1) or die(mysql_error());
Try inner query, I'm not sure following will work exactly as I cant test that now, but for getting result you have to use inner query. Inner query help me to get expected result in my case.
SELECT *
FROM entry p
WHERE id =
(SELECT max(id) FROM entry p2
WHERE p2.id = p.id)
GROUP BY p.date
ORDER BY p.id DESC;
This query will work for you:
create TABLE test (id INT PRIMARY KEY, tdate DATE, start INT);
SELECT t1.* FROM test as t1
LEFT JOIN test as t2
ON (t1.tdate = t2.tdate AND t1.id < t2.id)
WHERE t2.id IS NULL;
Try this query :-
select * from( select * from entry order by id DESC ) new Group by date
I'm having difficulty understanding how to Order a query by data from another table.
The existing query is: SELECT ID FROM UserTour WHERE Live = 1 ORDER BY LastUpdated DESC
This obviously Orders by the column 'LastUpdated' in the table 'UserTour'
However, I need it to be ordered by the column 'LastUpdated' which is in another table 'ImageLinks', Where 'TypeID' = 16 (again in 'ImageLinks').
I hope that makes sense.
So it would be something like: $ids = #mysql_values('SELECT ID FROM UserTour WHERE Live = 1 ORDER BY ('Select ID FROM 'ImageLinks' Where TypeID = 16 Order by LastUpdated DESC')');
Any help would be appreciated on how to do this. Cheers
If there is no relationship between the two tables your query in your question will look like this
select id from
(
SELECT
ID
, (Select ID FROM ImageLinks Where TypeID = 16
Order by LastUpdated DESC limit 1) as order_val
FROM UserTour
WHERE Live = 1
) x
ORDER BY x.order_val
which will work but will not do not any ordering as the order_val column will have a fixed value.
If the IDs are linked 1:1 (no indication that they are, but just supposin') we could do this:
select u.id
from UserTour u inner join ImageLinks i on u.ID = i.ID
where u.Live = 1 and i.TypeID = 16
order by i.LastUpdated desc
If the above is incorrect then you will have to decide how the two tables are related and join them correspondingly.
In other words, If the tables are in no way connected, then you cannot provide an ordering of one table's data based on a column in the other.
UPDATE
select
i.LinkID
, i.LastUpdated
from UserTour u inner join ImageLinks i
on u.ID = i.LinkID
where u.Live = 1 and i.TypeID = 16
group by i.LinkID, i.LastUpdated
order by i.LastUpdated desc LIMIT 30
I have three tables
1.course(c_id(pk),c_name,sem_no);
2.student(s_id(pk),s_name,user_name,password);
3.student_info(s_id(fk),c_id(fk));
I have logged in the student, THEN i run this query
SELECT distinct sem_no FROM course,student_info WHERE course.c_id=student_info.c_id and s_id='0001' ORDER BY sem_no ;
It shows all the semester he passed including the running semester..
now
I want to show the last value of sem_no column as his current semester..
how can I grab the last value of the sem_no?
Any help will be appreciated!
Here is an improved version of what you want:
SELECT sem_no
FROM course c join
student_info si
on c.c_id = si.c_id and s_id = '0001'
ORDER BY sem_no desc
LIMIT 1;
Note the use of proper ANSI join syntax. Also notice the use of table aliases, which make the query easier to read.
Try this:
SELECT sem_no
FROM course as t1 join
student_info as t2
on t1.c_id = t2.c_id
WHERE t2.s_id = '0001'
ORDER BY sem_no desc
LIMIT 1;
When selecting from mysql, you can order by either ascending or descending. ascending would be from 1-10 eg. and descending would be 10-1. So
SELECT distinct sem_no FROM course.student_info
WHERE course.c_id=student_info.c_id and s_id='0001' ORDER BY sem_no desc LIMIT 1;
would return last row, and only last row.
I'm trying to get the last ID# from the table called address_contact and the code I use is:
$result = mysql_query("SELECT id FROM address_contact ORDER BY lastUpdate DESC LIMIT 1")
or die(mysql_error());
$row = mysql_fetch_array( $result );
$id = .$row['id'];
Now I would like to close that connection and open a new one and then get all of the data from the following 3 tables using that ID# that we just got:
Table 1: address_address
Table 2: address_contact
Table 3: address_email
so it would look something like this ???
$result = mysql_query("SELECT address_contact.id,address_contact.lastname,address_contact.firstname,address_contact.primaryAddType,address_address.id,address_address.phone1,address_address.phone2,address_address.line2,address_email.id,address_email.email
FROM address_address
LEFT JOIN address_contact ON address_address.id=address_contact.id
LEFT JOIN address_email ON address_address.id=address_email.id
WHERE address_contact.id = ".$id)
But there has to be a easier/faster way of doing this?
If this ID is for a brand new record you'd just inserted, you should be using the msyql_last_insert_id() function, which guarantees that you get the last insert THIS particular script/database handle did. Your method is subject to race conditiosn - if some OTHER script does an insert behind this script's back, you'll get that script's ID, not yours.
That being said, you would be better off doing
SELECT max(id) FROM yourtable
instead of the order by/limit version. It's more efficient to it this way.
The basic code sequence would be:
INSERT INTO yourtable ....
SELECT #id := last_insert_id();
SELECT ... FROM yourtable WHERE id = #id;
You can do this in a single SQL statement using a sub query to find the ID.
SELECT
address_contact.id,
address_contact.lastname,
address_contact.firstname,
address_contact.primaryAddType,
address_address.id,
address_address.phone1,
address_address.phone2,
address_address.line2,
address_email.id,
address_email.email
FROM
address_address
LEFT JOIN address_contact ON address_address.id = address_contact.id
LEFT JOIN address_email ON address_address.id = address_email.id
WHERE address_contact.id = (
SELECT id FROM address_contact ORDER BY lastUpdate DESC LIMIT 1
)
Why not use single query
SELECT address_contact.id,address_contact.lastname,address_contact.firstname,address_contact.primaryAddType,address_address.id,address_address.phone1,address_address.phone2,address_address.line2,address_email.id,address_email.email
FROM address_address
LEFT JOIN address_contact ON address_address.id=address_contact.id
LEFT JOIN address_email ON address_address.id=address_email.id
ORDER BY address_contact.lastUpdate DESC
LIMIT 1