I have 2 tables. lets say Table and BackupTable
And i want to Update the id of Table with the last id from BackupTable.
because BackupTable holds all the data of Table. And table is being deleted after the data was inserted and then deleted.
This is what i have as a command.
update Table set id = CONCAT((SELECT id FROM BackupTable
ORDER BY BackupTable.id ASC) + id);
it gives me an error:
#1242 - Subquery returns more than 1 row
and from there i know how to add the data from Table to Backuptable.
nevermind...so stupid of me :)) i was selecting all the id's from that table
this is it.... i had to select the last inserted id.
update phubChannelRank set id = CONCAT((SELECT max(id) FROM ArchiveTheSeeker.phubChannelRank) + id);
Related
i have a table with much data in it. the id is set as a primary with AUTO_INCREMENT function. how can i insert a new row like
INSERT INTO `medikamente`(`id`, `Augmentin`, `Ciproxin`, `Klacid`, `Voltaren`, `Seractil`, `Mexalen`, `Aspirin`, `Thomapyrin`, `Esomeprazol`, `Omeprazol`, `Nexium`, `Pantoloc`, `Guttalax`, `indikator`, `indikator2`)
VALUES (NULL,'','','','','','','','','','','','','','','');
lets say between id 5 and id 6? i don't want to change the id's manualy to have a free row between that id's. how can i do that over an command?
br
You can get free place in id sequence by query:
UPDATE medikamente SET id = id + 1 WHERE id > 5 ORDER BY id DESC;
After you can use id 6 for insert new row:
INSERT INTO `medikamente`(
`id`, `Augmentin`, `Ciproxin`, `Klacid`, `Voltaren`, `Seractil`, `Mexalen`, `Aspirin`, `Thomapyrin`, `Esomeprazol`, `Omeprazol`, `Nexium`, `Pantoloc`, `Guttalax`, `indikator`, `indikator2`
) VALUES (
6,'','','','','','','','','','','','','','',''
);
Be carefully if you table have foreign keys.
Suppose I have a table with one column - called 'person' that contains a list of names. I want to find a specific person based off his index.
I tried using a sql variable to track each column index but the issue is - is that if I have a table of 5 records this will always output the 5th record.
SET #row_num = 0; SELECT #row_num := #row_num + 1 as row1 ,person FROM table;
SELECT row1 from table WHERE person = 'name'
I would recommend changing your database to add a second column for row_id. This is a fairly common practice. Then you can just use
SELECT * from table WHERE row_id = 3;
This will return the third row.
Another best possible way would be by means of a TEMPORARY TABLE as explained below
create a temp table
create temporary table temptab(ID INT NOT NULL AUTO_INCREMENT,Person VARCHAR(30))
Then insert data to temp table as
insert into temptab(Person) select Person from mytable
Then select the specific index person name from temp table like
select Person from temptab where ID = 5
I have a unique indexed column A with integer in it. I'd like it to increment by 1, in Oracle I do: update table set A=A+1 and it worked. But in mySQL, it give me the following error:
- MySQL Database Error: Duplicate entry '2' for key 1. I have three rows in the table with value: 1, 2 and 3 individually. I know why it gives me this error. But how do I solve this problem? Thanks.
You receive this error because your UPDATE TABLE SET A = A + 1, when updating the first ROW from 1 to 2 (1+1), it will get conflict with your second ROW, because there already is a ROW with ID = 2.
You have to do it descender from the last ROW to the first, do you have to alter your query to this:
UPDATE TABLE SET ID = ID + 1 ORDER By ID DESC;
The DESC clause will make the updates from the bottom of your table, so it won't find any duplicate ID in his path...
You can do this using an ORDER BY:
update table
set A=A+1
order by A desc
First we start with empty table
rows = 0
Second we insert random rows let say 3400
rows = 3400
For the third time i count how many rows are in the table, then insert the new rows and after that delete rows <= from the count.
This logic only work for the first time. If that repeat the count will always be 3400 but the id will increase so it will not delete the rows
I cant use last inserted ID since the rows are random and I dont how many it will load.
// Update
"SELECT count(*) from table" - the total count so far
"INSERT INTO tab_videos_watched (id , name) values (id , name)" - this is random can be 3400 or 5060 or 1200
"DELETE FROM table WHERE idtable <= $table_count"
If id is auto incremented, then you should use like:
select max(id) from my_table;
Read this maxId into a variable and then use when issued a delete query like:
delete from my_table where id <= ?;
Replace query parameter with the last found maxId value.
Alternatively you can define a column last_inserted as datetime type.
Before next insertions, select it into a local variable.
select max(last_inserted) as 'last_inserted' from my_table;
And after insertions are made, use the last_inserted to delete records.
delete from my_table where last_inserted <= ?;
Replace query parameter with the last found last_inserted value.
I have a MYSQL statement which inserts data into a row. The row id would be automatically incremented.
But how do I get back that row of data which got inserted and executed? That row of data should includes the row id which I no idea of what that is..
I want the whole row $row, not just the id. I don't know the id because it's autoincremented.
Since you have an autoincremented id you can use LAST_INSERT_ID()
E.g.
INSERT INTO supportContacts
(type, details)
VALUES
('Twitter', '#sqlfiddle');
SELECT * FROM
supportContacts
WHERE id = LAST_INSERT_ID();
DEMO
If you have an auto-incrementing column for the id, the following will work in any database:
select *
from table
where id = (select max(id) from table)
Under the assumption that no one else is inserting rows.
In SQL Server, you can do the following:
declare #id int;
insert . . .
select #id = ##Identity;
select *
from table
where id = #id;
Just note that the line with ##Identity needs to follow immediately after the insert.