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
Related
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.
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.
Upon submitting a review, I'm trying to run a query that deletes all rows with matching account number, except the eleven most recent. My table consists of "account","stamp"(datetime), and "reviews".
The account number is passed in the variable $account.
DELETE FROM table
WHERE account=?
AND stamp NOT IN (SELECT stamp FROM table ORDER BY stamp DESC LIMIT 11)
Not sure what is wrong with the query.
You can do this with a delete and join:
delete t
from table t left join
(select t.*
from table t
where account = ?
order by stamp desc
limit 11
) tt
on t.account = tt.account and t.stamp = tt.stamp
where t.account = ? and tt.account is null;
I have a simple table USERS:
id | name
----+------
Can you help me with the query that would fetch all rows from the table and:
a) Place 10 rows with highest PK values on top, in id DESC order;
b) Place all remaining rows ordered by name ASC order.
Thank you!
This is a bit of a tricky question. The approach I would take is a join approach. Identify the primary keys for the first group using a join (this is happily fast because you are working with primary keys). Then use the match to that table for the order by:
select t.*
from table t left outer join
(select id
from table t
order by id desc
limit 10
) t10
on t.id = t10.id
order by t10.id desc,
t.name asc;
First question would be: do you really need this in one single query? I'm really not seeing the use case for such a query to be honest.
It'd be easier to just fetch the 10 biggest ids (storing somewhere the 10th biggest), and then fetch the rest in ascending name order (with a restriction on ids being smaller than the 10th biggest).
Otherwise in a single query, something like this would work, but it doesn't seem very efficient to me (maybe someone will have a better idea).
(
SELECT
id, name
from
USERS
ORDER BY id DESC LIMIT 0,10
)
UNION
(
SELECT
id, name
from
USERS
WHERE
id NOT IN (
SELECT id, name from USERS ORDER BY id DESC LIMIT 0,10
)
ORDER BY name ASC
)
(or maybe with a NOT EXISTS - the inner query will be different - instead of the NOT IN)
I want to delete specific variables based on 'id' value. but the code below is displaying syntax error near: OFFSET 1. I use a similar code where I use SELECT instead of DELETE and it works fine, What am doing wrong here? Thanks
DELETE FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1 OFFSET 1
The offset component in LIMIT is not available in MySQL DELETE statements but it is allowed in SELECT statements.
So what you can do to get around this fact, is you can actually join a subselect in a DELETE operation, which will then give you your desired results:
DELETE a FROM users a
INNER JOIN
(
SELECT id
FROM users
WHERE name = '$name'
ORDER BY id
LIMIT 1,1
) b ON a.id = b.id
You cannot specify offset in DELETE's LIMIT clause.
Simple use:
DELETE FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1;
Try
DELETE FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1,1
You can not have OFFSET in DELETE query.