Update mysql id column according to timestamp column - mysql

I'm wondering if its possible to update all of the mysql table ids column based on timestamps?
So, I have table with columns ID,TEXT,STAMP, and IDs are really out of order. It's possible to sort table on STAMP and then update all of IDs incremental from 1 (so the oldest entry will have ID of 1) with one single query?

UPDATE table_name a,(SELECT id,(#newid:=#newid+1) AS d FROM table_name,(SELECT #newid:=0) AS f ORDER BY stamp) AS g SET a.id = g.d WHERE a.id = g.id;
i think this will do.

Related

MYSQL Delete all rows with the minimum DATE value

In MYSQL, I have a dataset which contains stock data of many many dates, and I want to delete all the rows that have the lowest date value (my date value is an integer, so in other words, I want to delete trades from the first date). I have found this code on stack overflow, but it only deletes one row, while I want to delete ALL rows with this minimum date.
This is the code I have found.
DELETE FROM TABLE
ORDER BY DATE
LIMIT 1
Thank you!
Use a query to find out minimum date then delete all records for this date.
-- MySQL
DELETE t
FROM TEST_TRADES t
INNER JOIN (SELECT MIN(COB_DATE) a
FROM TEST_TRADES) tt
ON t.COB_DATE = tt.a
Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4262ea268d2d9ba9b617142edf536cdc
I found solution to this question:
This is what I've done:
SELECT #MIN_DATE := MIN(DATE) FROM TABLE T;
DELETE FROM TABLE WHERE T.DATE = #MIN_DATE

count one column and update another in same table in mysql

I want to update one column based on count of another column in same table. I tried below query but no luck.
update
table
set conntype='Multiple'
where (select COUNT(cpemac) as nos from table group by cpemac) > 1
I get following error:
1093 - You can't specify target table 'table' for update in FROM clause
In MySQL, you need to use a JOIN:
update t join
(select cpemac, count(cpemac) as nos
from t
group by cpemac
) tt
on t.cpemac = tt.cpemac
set t.conntype = 'Multiple'
where cnt > 1;
This is a specific limitation of MySQL.
I should note, however, that your version would not work in any database. It would either update all rows or no rows, depending on the result of the subquery. There is no connection between the subquery and the outer query.

MYSQL matching added number query

Im doing MYSQL query.
Here's my table.
I need a query that results a row that contains value in added counts.
For example, if i'm looking for value of 20,000 counts, result row should be 3rd row where date is '2016-09-03'((8576+8694+8689)>20000).
There is something wrong in my table schema, but help me querying.
Thank you.
You're looking for such a query, assuming date is unique:
select counter.*
from counter
inner join counter c2
on counter.date >= c2.date
and c2.searchword = 'cryptocurrentcy'
where counter.searchword = 'cryptocurrency'
group by counter.date
having sum(c2.count) > 20000
order by counter.date
limit 1

Fetch entries in table from last using LIMIT a,b or limit offset

I want to fetch latest entries in a table that is containing more than 1,000,000 entries. I am using this query for an instance
SELECT id FROM tablea WHERE flag = "N" ORDER BY id LIMIT 510045,200;
and it gives me entries starting from 510045 and ending at 510245. Can MYSQL have something where I can get entries starting from 510245 to 510045. I mean fetching the data from the last and I don't want to fetch only 200 entries.
You should ORDER BY desc and, if you want, LIMIT for define how many entries you want.
Example:
SELECT id FROM tablea WHERE flag = "N" ORDER BY id DESC;
-- this will help to find the last entries
But if you want to have the latest entries that you didn't get in last query, you should always hold the value of the last ID, and use it as reference to next check.
Example (Supposing the last ID of the last query execution was 55304):
SELECT id FROM tablea WHERE flag = "N" WHERE id > 55304 ORDER BY id DESC;
If what you want is rows where the id is greater than 510245 just use the where condition
Select * FROM table WHERE flag = 'n' AND id > 510245
This should do it
As i understand your requirement . you may try it.
Select * FROM table WHERE flag = 'N' AND id > 510245 ORDER BY id
One more thing here is
The version i was working on was not supporting subquery containing LIMIT. So, #strawberry Thanks for giving me the hint to solve the question. But I used this sub query as inner join table(explained below)
SELECT id FROM tablea AS T1
INNER JOIN (SELECT id FROM tablea WHERE flag = "N" ORDER BY id LIMIT 510045,200) AS T2
WHERE T2._id = T1._id ORDER BY T2._id DESC;
This gave me the required results. Thanks everyone for your help !!

Mysql is throwing sql error on ORDER BY clause

It is absolutely beyond my ideas why the following query is not working:
DELETE orig FROM revision AS orig JOIN (
SELECT id
FROM revision
GROUP BY id
HAVING COUNT(*) > 1
) AS joined ON orig.id = joined.id
WHERE orig.id=1
ORDER BY orig.delta ASC
LIMIT 1
The error is thrown on line 8, so the ORDER BY clause. However, I cannot see anything wrong with it (the table revision does have a column called delta). Moreover, changing DELETE orig to SELECT * results in a flawlessly working command.
Is it possible that the delta-column isn't loaded? Or what is causing this error?
edit
Ah okay, I didn't know that you cannot use ORDER BY with multiple rows on a DELETE query.
Well, what I am trying to accomplish is that I delete one row in my table, which has an id that occurs more than once (will be 5 in production) and has the lowest delta of all rows with that id.
I.e. I have two rows in the revision table, both with the same id. I now want to delete that row which has the lowest delta of the two. This should be scalable, so that I delete all rows (with the same id) but one (the one with the highest delta).
You can't use ORDER BY if you have multiple tables in your DELETE statement
I think this is what you are trying to do. It will delete only 1 row only if the (id,delta) combination has a unique constraint:
DELETE orig
FROM revision AS orig
JOIN
( SELECT MIN(delta) AS delta
FROM revision
WHERE id = 1
HAVING COUNT(*) > 1
) AS joined ON orig.delta = joined.delta
WHERE orig.id = 1 ;
To delete all rows (with same id) except the (say 5) rows with highest delta, you can use:
DELETE orig
FROM revision AS orig
JOIN
( SELECT delta
FROM revision
WHERE id = 1
ORDER BY delta DESC
LIMIT 1 OFFSET 4 -- select the 5th highest
) AS joined ON orig.delta < joined.delta -- then find the lower than that
WHERE orig.id = 1 ;