update record in MySQL table using select statement - mysql

I'm trying to update the last inserted record and assign to it's column - campaign_id the value in id (same record).
i came up with this query :
UPDATE campaigns
SET campaign_id= (select id order by id desc LIMIT 1)
WHERE id = (select id order by id desc LIMIT 1)
but for some reason i can't understand it updated the ENTIRE table, why is that?

I think the following is what you were intending to do:
UPDATE campaigns
SET campaign_id = id
WHERE id = (SELECT id FROM campaigns ORDER BY DESC LIMIT 1);
Whether or not this be logically correct depends on whether the record with the max id is actually the latest record. I can imagine that not being the case, but perhaps the logic in the above subquery could be changed to cover this possibility.

Related

how to get first value of duplicate value in mysql

I have a table where I am having duplicates value also.
From that table, I want to get the first value of duplicate values via an order by id desc.
I am using below query to find count
select product_sku, quantity
from catalog_product_store_inventory
where ax_store_id=999
ORDER BY id DESC;
From this query, I get the all duplicates value.
I hope I made my query clear.
I am very new to MySQL.
What means duplicates in your case? Duplicates according to what product_sku or both product_sku,quantity - this should be used in GROUP BY clause:
SELECT product_sku,quantity
FROM catalog_product_store_inventory c
JOIN (
SELECT MAX(id) id
FROM catalog_product_store_inventory
GROUP BY product_sku
) m ON c.id = m.id
ORDER BY id DESC means that you want last ID from group and this one is MAX.

How to select the last record from MySQL table using SQL syntax if only a condition is meet

I have four fields in my db namely
Id(auto increment),
dept_id,
mat_code,
topic
I want to retrieve the last record in the database if a condition is meet. Am using dept_id for the condition.
The normal method is to order the result in DESC order and LIMIT the result set to 1 row
SELECT Id, dept_id, mat_code, topic
WHERE dept_id = 'something'
ORDER BY Id DESC
LIMIT 1
What is the condition? For what you want, I think a subquery is necessary:
SELECT t.*
FROM (SELECT t.*
FROM t
ORDER BY Id DESC
LIMIT 1
) t
WHERE dept_id = ??;
The subquery returns the last row (based on id). The outer WHERE determines if conditions are true.

Mysql: Delete from table where ID, except rows in a selection?

The problem
I'm using buddypress for Wordpress it has a table for private messages in side which are thread ids for message threads. Currently there's no limit on how many messages can be in a thread.
I want to create a command that deletes all but the most recent 10 messages in a thread. Below is the logic, but I'm not sure the correct syntax to do it?
Any of you mysql geniuses know the answer?
DELETE FROM TABLEA WHERE id = X delete everything with thread ID x
EXCEPT
(SELECT * FROM TABLEA WHERE id = X ORDER BY date_sent DESC LIMIT 10)
Selects most recent 10 I do not wish deleted.
This should working:
DELETE FROM TABLEA WHERE id = X AND id NOT IN (
SELECT TOP 10 id FROM TABLEA ORDER BY date_sent DESC
)
The sub-select of this query get the last 10 sent items. The main query have to delete the item with id X except the item is on the result of the sub-select.
I'm not sure how the table in buddypress works but I guess TABLEA should have its on primary key id. If TABLEA does have its own primary key id, here's my solution.
DELETE FROM TABLEA WHERE id = x AND TABLEA_id NOT IN
(SELECT TABLEA_id FROM TABLEA WHERE id = x ORDER BY date_sent DESC LIMIT 10)

Best practice to get newest row in a table

Consider a table like this:
The id column is set to auto-increment, update_time column is supposed to be the date that the row was inserted in the table.
I want to simply get the latest entry for a user with user_id = x, I found out there are some ways:
SELECT * FROM mytable WHERE user_id = x ORDER BY update_time DESC LIMIT 1
And
SELECT * FROM mytable WHERE user_id = x MAX(update_time)
And another query would be selecting the row with highest id number
I am not quite sure about the syntax of the later one (please correct me).
This may seem to be a trivial task, but there was a case that someone else has altered the auto_increment value on the table, and sometimes the time of the server has changed (well this has not been happened in my case but what if it does!!?)
What would be the safest query to get the latest entry row for a user_id ( I mean a query to return weigh, height and activity_level for a user with user_id = x)
Do I need to add more columns to the table? if so, what?
I like the join approach for this. Assumes that userID + update_time is unique...it'll pull back multiple rows if it isn't.
select user_id, max(update_time) maxtime from table group by user_id
Simple statement to get the max update time by user ID. Use it as a subquery with inner join (inner join will function as a filter)
select t.*
from table t
inner join
(select user_id, max(update_time) maxtime
from table group by user_id
--where user_id = x
)a
on a.user_id = t.user_id and a.maxtime = t.update_time
I commented out the where user_id = x line...the advantage to this method is you can get all users and their most recent at once.
SELECT * FROM mytable WHERE user_id = x AND update_time = (SELECT MAX(update_time) FROM mytable WHERE user_id = x)
This really does not have to be so complicated:
SELECT id
FROM table
WHERE user_id = x
ORDER BY update_time DESC
LIMIT 1
This version will be the quickest, simplest, and easiest to read. It is a win in every regard.

How to update the second to last entry in a MySQL table?

Does anyone have an idea how I can update, for example, a second to last entry?
For example, I have a Banned column in a Users table. I have to set Banned = 1 for the second to last account.
UPDATE Users SET Banned = '1' WHERE LIMIT 2,1
That's not working. Any ideas how I can get this to work?
Maybe it's possible to do one long query first. For example:
SELECT * FROM Users LIMIT 2,1
And then, using this query somehow update Banned column?
I assume there is an id auto-increment column in users table you can try this one
UPDATE Users SET Banned = '1' WHERE id =
(SELECT t.id FROM (SELECT id FROM Users ORDER BY id DESC LIMIT 1,1) t )
ORDER BY id DESC LIMIT 1,1 for second last account