I want to ignore duplicates (or update key on duplicates), but only on consecutive duplicates.
Say my initial table looks like this (uk and a are column names):
uk a
----
x 1
Iteration 1 inserts 1 so I want this to be ignored in order to avoid duplicates.
Iteration 2 inserts 2 and my table now looks like this:
uk a
----
x 1
x 2
Iteration 3 inserts 1 and because the last row where my unique key = x was different to 1 then I want 1 to be inserted again:
uk a
----
x 1
x 2
x 1
How can this be achieved in MySQL?
My current solution is to query in data from the table first and remove these duplicates, but I would prefer that it was handled by MySQL.
You need a column that will be an auto-increment primary key
CREATE TABLE your_table(
id INT NOT NULL AUTO_INCREMENT,
uk CHAR(30) NOT NULL,
a INT,
PRIMARY KEY (id)
);
INSERT INTO your_table(uk, a) VALUES ('x', 1)
now you can use the following insert command to avoid duplicates as you describe it
INSERT INTO your_table(uk, a)
SELECT 'x', 1 FROM your_table t1
JOIN (
SELECT max(id) maxid, uk
FROM your_table
GROUP BY uk
) t ON t.maxid = t1.id and
t1.a != 1 and
t.uk = 'x'
The second insert can be the following
INSERT INTO your_table(uk, a)
SELECT 'x', 2 FROM your_table t1
JOIN (
SELECT max(id) maxid, uk
FROM your_table
GROUP BY uk
) t ON t.maxid = t1.id and
t1.a != 2 and
t.uk = 'x'
The third insert will be the same as the first one and the result table will be as expected.
See the demo
From your table it is not visible, which values got inserted last. Data in a table is considered unordered. So for your last example we know there are the three records x|1, x|1, x|2 in the table, but not whether x|1 or x|2 was inserted last. You'd need an additional column to indicate this. This could be a datetime or an ascending ID.
If you don't want to change your table, you need a helper table containing the last record instead. Anyway, you'd write a before-insert trigger to look up the last inserted value and throw an error when the new record matches the last inserted record for th uk.
create table mytable(uk char(1), a int);
create table mytable_helper(uk char(1), a int, primary key (uk));
create trigger mytable_ins BEFORE INSERT ON mytable FOR EACH ROW
begin
DECLARE msg VARCHAR(128);
if exists (select * from mytable_helper h where new.uk = h.uk and new.a = h.a) then
set msg = concat('Consecutive duplicate for a = ', new.a, ' and uk = ''', new.uk, '''.');
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
else
replace into mytable_helper (uk, a) values (new.uk, new.a);
end if;
end;
insert into mytable (uk, a) values ('x', 1);
insert into mytable (uk, a) values ('x', 2);
insert into mytable (uk, a) values ('x', 1);
insert into mytable (uk, a) values ('y', 3);
insert into mytable (uk, a) values ('x', 1);
Error(s), warning(s):
Consecutive duplicate for a = 1 and uk = 'x'.
REXTESTER http://rextester.com/XOG65602
Related
How do I insert a sql row into a new table where it meets criteria but resets the id value. In other words, copy the row, but reset the id value.
This is my current sql
INSERT INTO followers_lost SELECT * FROM followers WHERE pk = $pk
I tried to SET id=null and VALUE (0), but both don't work.
All you have to do since you want all the columns except the identity is specify all the non-identity columns on the insert:
INSERT INTO [followers_lost] ([Column1],[column2]...{but not the identity
column})
SELECT [Column1],[column2]...{but not the identity column} FROM followers WHERE
pk = $pk
You can create the column and then update it:
SET #new_id=0;
UPDATE your_table
SET id = #new_id := #new_id + 1
where id = 0
OK based on your comment I think you are trying to take all or some of the fields from Followers except the PK and put them into Followers_Lost where they equal a certain PK. If you want multiple PK's you would need to change the where clause to an IN statement instead of an equal and adjust your values accordingly.
CREATE TABLE dbo.UAT_Followers_Lost (PK INT IDENTITY(1,1),DATA VARCHAR(50) )
CREATE TABLE dbo.UAT_Followers (PK INT IDENTITY(1,1),DATA VARCHAR(50) )
INSERT INTO dbo.UAT_Followers
(DATA)
SELECT 'Jan'
UNION ALL
SELECT 'Feb'
UNION ALL
SELECT 'Mar'
DECLARE #PK INT
SET #PK = 1
INSERT INTO dbo.UAT_Followers_Lost
(Data)
SELECT Data
FROM dbo.UAT_Followers
WHERE PK = #PK
I have a table in mysql like this (the id is primary key):
id | name | age
1 | John | 46
2 | | 56
3 | Jane | 25
Now I want to update the name only if this is empty. If the value is not empty it should duplicate the row with a new id else it should update the name.
I thought it could be done with an if-statement but it doesn't work.
if((select `name` from `table1` where `id` = 3) = '',
update `table1` set `name`='ally' where `id` = 3,
insert into `table1` (`id`,`name`,`age`) values
(4, 'ally', select `age` from `table1` where `id` = 3))
EDIT:
With Spencers answer I made it working using an if in the code. (However I would still like a way to do just a single mysql query).
db.set_database('database1')
cursor = db.cursor()
query = "select IF(CHAR_LENGTH(name)>0,1,0) from table1 where id = {0}".format(id)
cursor.execute(query)
val1 = cursor.fetchone()
if val1[0]:
query = "INSERT INTO `table1` (`id`,`name`,`age`) SELECT {0},{1},`age` FROM `table1` WHERE `id` = {2}".format(new_id, name, id)
cursor.execute(query)
else:
query = "update `table1` set `name` = '{0}' where `id` = {1}".format(name, id)
cursor.execute(query)
db.commit()
If you make like this :
select t.*,
if(
EXISTS(select n.name from table1 n where n.id = 2 and NULLIF(n.name, '') is null) ,
'true',
'false'
) from table1 t
if statement returns "true", becouse in your table exist row where id =2 and name is empty.
like this example, You can edit your query :
if(
EXISTS(select n.name from table1 n where n.id = 3 and NULLIF(n.name, '') is null),
update `table1` set `name`='ally' where `id` = 3,
insert into `table1` (`id`,`name`,`age`) values
(4, 'ally', select `age` from `table1` where `id` = 3)
)
IF is not a valid MySQL statement (outside the context of a MySQL stored program).
To perform this operation, you'll need two statements.
Assuming that a zero length string and a NULL value are both conditions you'd consider as "empty"...
To conditionally attempt an update of the name field, you could do something like this:
UPDATE table1 t
SET t.name = IF(CHAR_LENGTH(t.name)>0,t.name,'ally')
WHERE t.id = 3 ;
The IF expression tests whether the current value of the column is "not empty". If it's not empty, the expression returns the current value of the column, resulting in "no update" to the value. If the column is empty, then the expression returns 'ally'.
And you'd need a separate statement to attempt an INSERT:
EDIT
This isn't right, not after a successful UPDATE... of the existing row. The attempt to INSERT might need to run first,
INSERT INTO table1 (id,name,age)
SELECT 4 AS id, 'ally' AS name, t.age
FROM table1 t
WHERE t.id = 3
AND CHAR_LENGTH(t.name)>0;
We need a conditional test in the WHERE clause that prevents a row from being returned if we don't need to insert a row. We don't need to insert a row if the value 'ally' ...
The use of CHAR_LENGTH >0 is a convenient test for string that is not null and is not zero length. You could use different test, for however you define "empty". Is a single space in the column also considered "empty"?)
Okay here is the situation:
I the following data in a table.
PAIR_NO NO NO2
3 5678EFGH 1234ABCD
4 1111BBBB 0000AAAA
1 1234ABCD 5678EFGH
2 0000AAAA 1111BBBB
The constraints are if no = no2 in another row skip that row.
So in this sample data the only rows that would be selected should be pair no 3 and 4.
I have tried to merge and inner join with self but I just keep getting all 4 rows back.
I have tried to insert into a table where not exists but again I get 4 rows inserted.
SELECT a.* from PAIRS a
inner join PAIRS b on a.no=b.no2 and a.no2=b.no;
I was thinking maybe selecting distinct number from column 1 and then check those in column 2 but I think that would yield the same four rows.
I may be over thinking this problem and maybe some here can look at this and see where the solution is hiding.
I am currently testing this on MySQL but it should run on SQLServer 2008. I have searched but all the questions didn't seem to match my data set issue.
Taking you at your word, meaning selecting all records where the value of no column does not appear anywhere in no2 column in the same table, try this:
SELECT A.PAIR_NO, A.NO, A.NO2
FROM PAIRS A
LEFT JOIN PAIRS B ON(A.NO = B.NO2)
WHERE B.PAIR_NO IS NULL -- assuming this column is not nullable
Another option is to use NOT EXISTS:
SELECT PAIR_NO, NO, NO2
FROM PAIRS A
WHERE NOT EXISTS(
SELECT 1
FROM PAIRS B
WHERE B.NO2 = A.NO
)
I personally prefer the LEFT JOIN option since it's shorter and more readable.
Both of these statement should work on both MySql and Sql Server.
Okay fellas I want to thank you all for helping, but I think I solved my issue. Took me a second but I believe this is what I am after (SQL Server 2008):
if OBJECT_ID('tempdb..#pairs') is not null drop table #pairs
if OBJECT_ID('tempdb..#pairs_final') is not null drop table #pairs_final
create table #pairs(pair_no int, a_no varchar(17),a_no2 varchar(17))
create table #pairs_final(pair_no int Identity(1,1), a_no varchar(17),a_no2 varchar(17))
insert into #PAIRS values(1,'1234ABCD','5678EFGH');
insert into #PAIRS values(1,'1234ABCD','XXXX9999');
insert into #PAIRS values(2,'0000AAAA','1111BBBB');
insert into #PAIRS values(3,'5678EFGH','1234ABCD');
insert into #PAIRS values(4,'1111BBBB','0000AAAA');
insert into #PAIRS values(1,'XXXX9999','1234ABCD');
insert into #pairs_final
select a.a_no,a.a_no2 from #pairs a
join (
select distinct a_no_p from(
select pair_no,a_no_p,
ROW_NUMBER() over (partition by pair_no order by a_no_p) as RN
from #pairs
unpivot(
a_no_p for clms in (a_no2,a_no)
) as umpvt
) as mypairs
where RN = 1
) as my_pairs on my_pairs.a_no_p=a.a_no
select * from #pairs_final
This will give me the following results:
pair_no a_no a_no2
1 1234ABCD 5678EFGH
2 1234ABCD XXXX9999
3 0000AAAA 1111BBBB
Hope this might help someone else.
Enjoy.
DECLARE #TBL AS TABLE
(
[NO] INT,
[CODE] VARCHAR(50),
[AREA] VARCHAR(50)
)
/* EXAMPLE 1 */
INSERT INTO #TBL([NO],[CODE],[AREA]) VALUES (1,'001','A00')
INSERT INTO #TBL([NO],[CODE],[AREA]) VALUES (2,'001','A00')
INSERT INTO #TBL([NO],[CODE],[AREA]) VALUES (3,'001','B00')
INSERT INTO #TBL([NO],[CODE],[AREA]) VALUES (4,'001','C00')
INSERT INTO #TBL([NO],[CODE],[AREA]) VALUES (5,'001','C00')
INSERT INTO #TBL([NO],[CODE],[AREA]) VALUES (6,'001','A00')
INSERT INTO #TBL([NO],[CODE],[AREA]) VALUES (7,'001','A00')
/* EXAMPLE 2 */
/* ***** USE THIS CODE TO ENTER DATA FROM DIRECT TABLE *****
SELECT
ROW_NUMBER() OVER(ORDER BY [FIELD_DATE]) AS [NO]
,[FIELD_CODE] AS [CODE]
,[FIELD_AREA] AS [AREA]
FROM TABLE_A
WHERE CAST([FIELD_DATE] AS DATE) >= CAST('20200307' AS DATE)
ORDER BY [FIELD_DATE],[FIELD_CODE]
*/
SELECT
A.NO AS ANO
,A.CODE AS ACODE
,A.AREA AS AAREA
,B.NO AS BNO
,B.CODE AS BCODE
,B.AREA AS BAREA
,CASE WHEN A.AREA=B.AREA THEN 'EQUAL' ELSE 'NOT EQUAL' END AS [COMPARE AREA]
FROM #TBL A
LEFT JOIN #TBL B
ON A.NO=B.NO+1
I have a table on which id is a primary key column set with auto increment. It contains over 10,00 rows.
I need to get all primary keys that have been deleted.
like
1 xcgh fct
2 xxml fcy
5 ccvb fcc
6 tylu cvn
9 vvbh cvv
The result that i should get is
3
4
7
8
currently i count all records and then insert(1 to count) in another table and then i select id from that table that dosent exists in record table. But this method is very inefficient. Is there any direct query that i can use?
please specify for mysql.
See fiddle:
http://sqlfiddle.com/#!2/edf67/4/0
CREATE TABLE SomeTable (
id INT PRIMARY KEY
, mVal VARCHAR(32)
);
INSERT INTO SomeTable
VALUES (1, 'xcgh fct'),
(2, 'xxml fcy'),
(5, 'ccvb fcc'),
(6, 'tylu cvn'),
(9, 'vvbh cvv');
set #rank = (Select max(ID)+1 from sometable);
create table CompleteIDs as (Select #rank :=#rank-1 as Rank
from sometable st1, sometable st2
where #rank >1);
SELECT CompleteIDs.Rank
FROM CompleteIDs
LEFT JOIN someTable S1
on CompleteIDs.Rank = S1.ID
WHERE S1.ID is null
order by CompleteIDs.rank
There is one assumption here. That the number of records in someTable* the number of records in sometable is greater than the maximum ID in sometable. Otherwise this doesn't work.
You can try to create a temp table, fill it with e.g. 1,000 values, you can do it using any scripting language or try a procedure (This might be not-effective overall)
DELIMITER $$
CREATE PROCEDURE InsertRand(IN NumRows INT)
BEGIN
DECLARE i INT;
SET i = 1;
START TRANSACTION;
WHILE i <= NumRows DO
INSERT INTO rand VALUES (i);
SET i = i + 1;
END WHILE;
COMMIT;
END$$
DELIMITER ;
CALL InsertRand(5);
Then you just do query
SELECT id AS deleted_id FROM temporary_table
WHERE id NOT IN
(SELECT id FROM main_table)
Please note that it should be like every day action or something cause it's very memory inefficient
I have an auto increment column ID, and for some situation I wanted the other column to be equal to the primary key + 1 value
ID | other
1 | 2
2 | 3
3 | 4
4 | 123 (some situation, it is not always plus 1)
How can I achieve this?
Here's what I have tried
INSERT INTO table (`ID`,`other`) VALUES ('',(SELECT MAX(ID)+1 FROM table))
But that returns an error
You can't specify target table 'table' for update in FROM clause
Try Below query:
ALTER TABLE dbo.table ADD
Column AS ([ID]+1)
GO
It will definitely work
Using a normal AUTO_INCREMENT column as id, I cannot think of a way to do this in MySQL. Triggers, which otherwise would have been an option, don't work well with AUTO_INCREMENT columns.
The only way I see is to do two commands for an INSERT;
INSERT INTO bop (value) VALUES ('These values should be 1 and 2');
UPDATE bop SET other = id+1 WHERE id = LAST_INSERT_ID();
An SQLfiddle to test with.
The closest I'm getting to what you're looking for is to generate sequences separately from AUTO_INCREMENT using a function, and use that instead to generate the table id;
DELIMITER //
CREATE TABLE bop (
id INT UNIQUE,
other INT,
value VARCHAR(64)
)//
CREATE TABLE bop_seq ( seq INT ) // -- Sequence table
INSERT INTO bop_seq VALUES (1) // -- Start value
CREATE FUNCTION bop_nextval() RETURNS int
BEGIN
SET #tmp = (SELECT seq FROM bop_seq FOR UPDATE);
UPDATE bop_seq SET seq = seq + 1;
RETURN #tmp;
END//
CREATE TRIGGER bop_auto BEFORE INSERT ON bop
FOR EACH ROW
SET NEW.id = bop_nextval(), NEW.other=NEW.id + 1;
//
That'd let you do inserts and have it autonumber like you want. The FOR UPDATE should keep the sequence transaction safe, but I've not load tested so you may want to do that.
Another SQLfiddle.
I solved this by updating 2 times the DB..
I wanted to do +1 from 19 till ..
UPDATE `table` SET `id`=`id`+101 WHERE id <= 19
UPDATE `table` SET `id`=`id`-100 WHERE id <= 119 AND id >= 101