I have a table with columns:
[id,name,public_id]
actually the table contains
[1,john,0]
[2,sara,0]
[3,jack,0]
.....
I want to change the third column to 1050,1051,1052....
[1,john,1050]
[2,sara,1051]
[3,jack,1052]
.....
How can I make that update?
Some considerations: The public id must be over 1049 and must be consecutive. For example for 100 rows the public_id must be [1050....1149]
Thanks in advance
You can do an update like this:
UPDATE table SET public_id = id + 1049
With this, you are using the ID, and adding 1049 to it to get the result you need
Assuming your table's name is TEST_TABLE, this MySQL syntax will update PUBLIC_ID with consecutive values starting from 1500 (and in order of ID):
REPLACE INTO TEST_TABLE
SELECT TEST_TABLE.ID, TEST_TABLE.NAME, #ROWNUM := #ROWNUM + 1
FROM
TEST_TABLE,
(SELECT #rownum := 1499) R
ORDER BY ID
In plain English:
For each row, figure out its order when data is sorted by ID. Call this order "ROWNUM".
Put ROWNUM (+ starting offset) back to the table instead of the original PUBLIC_ID.
WARNING: According to MySQL documentation, REPLACE will actually delete a duplicated row before inserting it again (instead of just updating modified fields), which my be an issue in the presence of foreign keys.
(Not sure about SQLite, but I'm guessing you could employ a similar general idea.)
Related
It's been my first question to this website, I'm sorry if I used any wrong keywords. I have been with one problem from quite a few days.
The Problem is, I have a MYSQL table named property where I wanted to add a ref number which will be a unique 6 digit non incremental number so I alter the table to add a new column named property_ref which has default value as 1.
ALTER TABLE property ADD uniqueIdentifier INT DEFAULT (1) ;
Then I write a script to first generate a number then checking it to db if exist or not and If not exist then update the row with the random number
Here is the snippet I tried,
with cte as (
select subIdentifier, id from (
SELECT id, LPAD(FLOOR(RAND() * (999999 - 100000) + 100000), 6, 0) AS subIdentifier
FROM property as p1
WHERE "subIdentifier" NOT IN (SELECT uniqueIdentifier FROM property as p2)
) as innerTable group by subIdentifier
)
UPDATE property SET uniqueIdentifier = (
select subIdentifier from cte as c where c.id = property.id
) where property.id != ''
this query returns a set of record for almost all the rows but I have a table of entries of total 20000,
but this query fills up for ~19000 and rest of the rows are null.
here is a current output
[current result picture]
If anyone can help, I am extremely thanks for that.
Thanks
Instead of trying to randomly generate unique numbers that do not exist in the table, I would try the approach of randomly generating numbers using the ID column as a seed; as long as the ID number is unique, the new number will be unique as well. This is not technically fully "random" but it may be sufficient for your needs.
https://www.db-fiddle.com/f/iqMPDK8AmdvAoTbon1Yn6J/1
update Property set
UniqueIdentifier = round(rand(id)*1000000)
where UniqueIdentifier is null
SELECT id, round(rand(id)*1000000) as UniqueIdentifier FROM test;
I allready found part of the solution in the following thread:
Removing duplicate email address based on the lowest id in mysql
I have this MYSQL table that contains also email addresses from which i want the duplicates to be removed witch leaving the duplicate with the highest ID intact.
There are also many empty email-fields that should be ignored and thus preserved.
After that no duplicate entries should ever be allowed for the emailfield in the future.
How can i achieve this? My head is absolutely spinning over this and i can'figure it out.
I want to do this from within phpmyadmin since it should be a onetime operation only.
Coming from the following link: MySQL remove duplicates from big database quick
I tried:
create table tmp like yourtable;
alter table tmp add unique (text1, text2);
insert into tmp select * from yourtable
on duplicate key update text3=ifnull(text3, values(text3));
rename table yourtable to deleteme, tmp to yourtable;
drop table deleteme;
But that gave me a syntax error concerning the select * statement (3rd line).
Try :
insert into tmp (email) select X.email from ((SELECT email FROM yourtable WHERE LENGTH(email) = 0) UNION (SELECT email FROM yourtable WHERE LENGTH(email) > 0 GROUP BY email ORDER BY id DESC)) AS X;
I am trying to insert records into MySQL database from a MS SQL Server using the "OPENQUERY" but what I am trying to do is ignore the duplicate keys messages. so when the query run into a duplicate then ignore it and keep going.
What ideas can I do to ignore the duplicates?
Here is what I am doing:
pulling records from MySQL using "OpenQuery" to define MySQL "A.record_id"
Joining those records to records in MS SQL Server "with a specific criteria and not direct id" from here I find a new related "B.new_id" record identifier in SQL Server.
I want to insert the found results into a new table in MySQL like so A.record_id, B.new_id Here in the new table I have A.record_id set as a primary key for that table.
The problem is that when joining table A to Table B some times I find 2+ records into table B matching the criteria that I am looking for which causes the value A.record_id to 2+ times in my data set before inserting that into table A which causes the problem. Note I can use aggregate function to eliminate the records.
I don't think there is a specific option. But it is easy enough to do:
insert into oldtable(. . .)
select . . .
from newtable
where not exists (select 1 from oldtable where oldtable.id = newtable.id)
If there is more than one set of unique keys, you can add additional not exists statements.
EDIT:
For the revised problem:
insert into oldtable(. . .)
select . . .
from (select nt.*, row_number() over (partition by id order by (select null)) as seqnum
from newtable nt
) nt
where seqnum = 1 and
not exists (select 1 from oldtable where oldtable.id = nt.id);
The row_number() function assigns a sequential number to each row within a group of rows. The group is defined by the partition by statement. The numbers start at 1 and increment from there. The order by clause says that you don't care about the order. Exactly one row with each id will have a value of 1. Duplicate rows will have a value larger than one. The seqnum = 1 chooses exactly one row per id.
If you are on SQL Server 2008+, you can use MERGE to do an INSERT if row does not exist, or an UPDATE.
Example:
MERGE
INTO dataValue dv
USING tmp_holding_DataValue t
ON t.dateStamp = dv.dateStamp
AND t.itemId = dv.itemId
WHEN NOT MATCHED THEN
INSERT (dateStamp, itemId, value)
VALUES (dateStamp, itemId, value)
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
I have a large table of data with a new field added called uniq_id what I am looking for is a query I can run that will update and increment this field for each row, without having to write a script to do this.
Any ideas?
somethink like that (hard to make better without your structure)
SET #rank = 0;
UPDATE <your table> JOIN (SELECT #rank:= #rank+ 1 AS rank, <your pk> FROM <your table> ORDER BY rank DESC)
AS order USING(<your pk>) SET <your table>.uniq_id = order.rank;
or easier
SET #rank=0;
UPDATE <your table> SET uniq_id= #rank:= (#rank+1) ORDER BY <anything> DESC;
What other columns does the table have? If you define the new column with AUTO_INCREMENT MySQL will fill it with sequential values starting from 1. There's a catch though: the column has to be a key, or at least a part of the key (see the documentation for details). If you don't have a primary key in the table already, you can simply do this:
alter table MyTable add uniq_id int auto_increment, add primary key (uniq_id);
If you can't change the keys in the table and just want to fill the values of the new column as a one-off thing, you can use this update statement:
update MyTable set uniq_id = (#count := coalesce(#count+1, 1));
(coalesce returns its first non-null argument; it effectively establishes the value of the column for the first row.)