get latest data from table - mysql

I am using jdbc to insert and getvalues form my database.
Suppose my sql table is like this-
NAME | STATE
x in
y in
z in
x out
y out
z out
x in
y in
As you can see there are similar values in the above table. I know how to normally get the value from table but i want to get the most recent added value of lets say 'z' which was added to the table or the last entry of z from the above table how can i get it?
Another column of type timestamp is there but when you are getting the value you only have name 'z' you don't know when the last entry was in the table

SELECT * FROM TABLE ORDER BY `timestamp` DESC LIMIT 1

Related

Delete date column with varchar(255)

I want to delete data which are past 2years. filed name is Date and type is varchar(255)
delete from <table_name> where <Filed> like '%2022';
running very longtime but no deletion of data
I have check and tried the query, you can try with
DELETE From <datatable> WHERE <date> LIKE '%2022';
DELETE From post WHERE date LIKE '%2022'; #Example
May you provide the database or screenshot? I have tried the query and no issue https://www.db-fiddle.com/f/syhtgVyEcSPcHRXBXHLtor/0
If primary key(probably id) and the date column are correlated, meaning bigger id will result the later dates(in this case, it is a of type varchar, and thanks to P.Salmon for pointing this out),then
I think you can delete using primary key(normally it is column id), for example:
select id from table where date > '2020' order by id asc limit 1;
// assume this id = 123456789, and delete rows that created before this id was created
delete from table where id < 123456789;
if there is not correlation, I have some ideas like below:
create a new column called created_at of type year/date/datetime/timestamp(probably date or year will do), it will store the actual year or date or datetime, use it to replace the date column of type varchar, probably create an index on created_at, and delete with the new column
If there is a index on date(varchar), since the % sign in like clause will cause the server not using index, so it is a full table scan for sure, and can you like enumerate all date like '01-01-2020', '01-02-2020', and delete rows one date by one date, with a script, I think in this way at least you get to use the index
if there are too many rows, like 10 years or even more, is it possible just migrate data within 2 years to a new table, and just remove the old table?
write a script, fetch 10000 row each time from beginning of primary key, and delete those that are over 2 years, and fetch next 10000
last_id = 0
select * from table where id > last_id order by id asc limit 10000;
last_id = [last id of the query]
delete from table where id in (xxx);

How to give single value to all columns of a field in a table?

suppose in my table i have a field Normal for this field , i have to insert same value from row 1 to 100 ,how can i give with single query please tell me guys.
You can try to use the Update query like this:
UPDATE mytable
SET myColumn = 'yourValue'
WHERE id BETWEEN 1 AND 100;
If you want to update only the rows which have id between 1 and 100 then add a where clause condition as well. And if it is for all the rows then you can remove the where condition.
INSERT INTO tbl_name
(normal)
VALUES
(value),(value),(value),(value),(value)
repeat value 100 times
The following will update the normal of the 1st 100 rows of your table based on the order by (I chose Id as generally this would be the 1st 100 rows added, other options are things like created date if your table does not have a primary key.)
Update yourtable
set Normal = xxxxxx
where ROW_NUMBER() OVER(ORDER BY ID) <= 100
This is more accurate than using
Id between 1 and 100
as that does not take into consideration deleted records from 'yourtable'.

Access: Search and Update Query

I'm running a query in access to search a table with two fields of numbers. One field is "Dose" and the other is "Volume". Below I'm searching for the "Dose" when the "Volume" is equal to a value nearest to 2 and dividing by 100 to get the correct units, which works fine.
SELECT TOP 1 [Table1]![Dose]/100 AS CentiDose
FROM [Table1]
ORDER BY Abs([Table1]![Volume]-2);
However I want to use UPDATE (or at least that's what I think?) to take this value and insert it into a field of one record in another table ie Table2, record 1, field 1. Is this possible? Any help would be greatly appreciated as I'm a novice at using Access.
If you're looking to update an existing row in table2 then UPDATE is what you want, something like
UPDATE Table2
SET Field1 = Centidose
WHERE Table2.recordid = x
If you need to append a new record to table2, it would look more like
INSERT INTO Table2
SET Field1 = Centidose
WHERE Table2.recordid = x

I need to copy the top 10 rows from a table and add it to same table, but id column is primary key??? how can I do this?

I need to copy the top 50 rows from the table named T_myorders which has 100 rows, and add it to same table, but the id column is primmary key, which is not letting me to do this, and I need the output in such a way when I add the rows to my table the added rows should start the id column with 101,102,103.... instead of again printing same 1,2,3....This one step and another one is,
I should declare two values one is how many top rows I need to copy and another value is how many times it should repeat, say 2, then the 50 rows should repeat two times and id column should be upto 200.
Can anyone help me in this task.
Thanks,
Raj
create a temp table
insert top x records (where x is first input parameter) into temp table using select insert.
find the maximum primary key value, increament it by 1 and store it in a variable primary_key
Declare a cursor to fetch records from temp table
Create a for loop which runs for y times (where y is 2nd parameter)
inside for loop
open the cursor and read rows
loop
insert the records in to the main table, use primary_key as a primary key
reassign the primary_key = primary_key +1
close loop
close the cursor
close the for loop
Hope this helps. This is just an algorithim which might be helpful
This can be done in a Better way
you can avoid cursor by using below statement create a temp table
insert top x records (where x is first input parameter) into temp table using select insert.
find the maximum primary key value, store it in a variable primary_key
Create a for loop which runs for y times (where y is 2nd parameter)
inside for loop
insert into YOUR_TABLE SELECT IDENTITY(int, primary_key ,1) AS id, REST OF COLUMNS from temp table
reset the primary_key to hold the new value
close loop
declare #s1 as int
select #s1=MAX(primary key) from Table
insert into Table
select top 50 ROW_NUMBER() over (partition by 0 order by f1)+#s1 as pk_id (instead of primary key) ,f1,f2,f3 from Table

mysql update int column from 2 other int columns where result is a negative

I have 3 columns a,b,c. A = 3 B =4, I'm trying to perform an update statement that can handle negative. For example in this case i should get -1, but instead I get the error Out of range value for column 'C' at row 1. All columns are ints.
Any suggestions would be awesome!
update tbl
set c = a - b
where uniqueID = 49
Check column C definition, it might be set as UNSIGNED.
EDIT
Check definition of all columns involved in the calculation. If any of them is marked as UNSIGNED, then all calculations will be done usign UNSGINED arithmetic with can result in the Out of range value error.