Hi all I would like to get all the Id's that are newly inserted. Actually I am inserting the data in to a table based on foreign key id.
Insert into table()
Values(SELECT * FROM table where fId=X)
Here the two tables are same, so after inserting the data I would like to return all the Identities that got inserterd
You need to use OUTPUT (https://msdn.microsoft.com/en-us/library/ms177564.aspx) clause in your insert. Here's a working example:
DECLARE #tmp TABLE (id INT IDENTITY(1,1), txt VARCHAR(100))
INSERT INTO #tmp (txt)
OUTPUT INSERTED.id
VALUES ('a'),('b')
So for your query - which is not a valid insert statement - it would be:
Insert into table()
OUTPUT INSERTED.id_column_you_need_to_be_returned
Values(SELECT * FROM table where fId=X)
Related
So I read the other posts but this question is unique. So this SQL dump file has this as the last entry.
INSERT INTO `wp_posts` VALUES(2781, 3, '2013-01-04 17:24:19', '2013-01-05 00:24:19'.
I'm trying to insert this value to the table...
INSERT INTO `wp_posts` VALUES(5, 5, '2005-04-11 09:54:35', '2005-04-11 17:54:35'
it gives me the error, "Column count doesn't match value count at row 1." So I'm lost on the concept of how the column and row apply here.
Doesn't 2781,3 mean row 2781 and column 3? And doesn't 5,5 mean row 5 and column 5?
The error means that you are providing not as much data as the table wp_posts does contain columns. And now the DB engine does not know in which columns to put your data.
To overcome this you must provide the names of the columns you want to fill. Example:
insert into wp_posts (column_name1, column_name2)
values (1, 3)
Look up the table definition and see which columns you want to fill.
And insert means you are inserting a new record. You are not modifying an existing one. Use update for that.
you missed the comma between two values or column name
you put extra values or an extra column name
You should also look at new triggers.
MySQL doesn't show the table name in the error, so you're really left in a lurch. Here's a working example:
use test;
create table blah (id int primary key AUTO_INCREMENT, data varchar(100));
create table audit_blah (audit_id int primary key AUTO_INCREMENT, action enum('INSERT','UPDATE','DELETE'), id int, data varchar(100) null);
insert into audit_blah(action, id, data) values ('INSERT', 1, 'a');
select * from blah;
select * from audit_blah;
truncate table audit_blah;
delimiter //
/* I've commented out "id" below, so the insert fails with an ambiguous error: */
create trigger ai_blah after insert on blah for each row
begin
insert into audit_blah (action, /*id,*/ data) values ('INSERT', /*NEW.id,*/ NEW.data);
end;//
/* This insert is valid, but you'll get an exception from the trigger: */
insert into blah (data) values ('data1');
MySQL will also report "Column count doesn't match value count at row 1" if you try to insert multiple rows without delimiting the row sets in the VALUES section with parentheses, like so:
INSERT INTO `receiving_table`
(id,
first_name,
last_name)
VALUES
(1002,'Charles','Babbage'),
(1003,'George', 'Boole'),
(1001,'Donald','Chamberlin'),
(1004,'Alan','Turing'),
(1005,'My','Widenius');
You can resolve the error by providing the column names you are affecting.
> INSERT INTO table_name (column1,column2,column3)
`VALUES(50,'Jon Snow','Eye');`
please note that the semi colon should be added only after the statement providing values
In my case i just passed the wrong name table, so mysql couldn't find the right columns names.
Is it possible to insert multiple data in a single mysql row?
Example of multiple id:
41,32,31,293,877
Yes it is possible to insert the multiple values at a time in mysql database.
suppose you have created a table of name emp. and in this table you have 1 field which is named as id.
now you just want to insert the multiple id in a table named emp , so can do this by writing following command ->
INSERT INTO emp(id) VALUES(1),(2),(3),(4),(5),(6);
Above query will insert the following given values in a table.
If you declare column to be VARCHAR().
yes you can insert multiple IDs. like
INSERT INTO table (Id) value ('41,32,31,293,877');
If you are using varchar or text data type for Id field then it is possible to enter and store with comma separated.
INSERT INTO TABLENAME (Id) VALUES ('41,32,31,293,877');
If you are inserting into a single table, you can write your query like this :
insert into table (id) values (41),(32),(31),(293),(877);
I looked into MySQL duplicate key but cant figure it out.
I have a table like below:
id series chapter path(can be unique)
I want only insert data and not update. Lets say I have data like below:
seri:Naruto, klasor:567 ==> If both of these exist in table then do not insert.
seri:Naruto, klasor:568 ==> If Naruto exist but 568 does not exist then do insert.
How can I achieve this?
Easiest way would be to define unique index with two columns on that table:
ALTER TABLE yourtable ADD UNIQUE INDEX (seri,klasor);
You may also define two column primary key, which would work just as well.
Then use INSERT IGNORE to only add rows when they will not be duplicates:
INSERT IGNORE INTO yourtable (seri, klasor) VALUES ('Naruto',567);
INSERT IGNORE INTO yourtable (seri, klasor) VALUES ('Naruto',568);
Edit: As per comments, you can't use UNIQUE INDEX which complicates things.
SET #seri='Naruto';
SET #klasor=567;
INSERT INTO yourtable
SELECT seri,klasor FROM (SELECT #seri AS seri, #klasor AS klasor)
WHERE NOT EXISTS (SELECT seri, klasor FROM yourtable WHERE seri=#seri AND klasor=#klasor);
You may use the above query with two local variables or convert it to single statement by replacing the local variables with actual values.
Better way would be to use stored procedure:
CREATE PROCEDURE yourinsert (vseri VARCHAR(8), vklasor INT)
BEGIN
DECLARE i INT;
SELECT COUNT(*) INTO i FROM yourtable WHERE seri=vseri AND klasor=vklasor;
IF i=0 THEN
INSERT INTO yourtable (seri,klasor) VALUES (vseri, vklasor);
END IF;
END;
This would allow you to perform the INSERT using:
CALL yourinsert('Naruto',567);
INSERT INTO table_name (seri, klasor) VALUES ('Naruto',567)
WHERE NOT EXISTS( SELECT seri,klasor FROM table_name WEHERE seri='Naruto' AND klasor=567
)
Hope this helps..
I need to create a SQL script with many step in it.
First of all, I need to insert data into a Parent Table.
How can I Get the list of primary key value
Here is an example of what I'm trying to perform.
MyParentTable
MyParentID PK
col1,
col2,
col3
--INSERT VALUE INTO THE PARENT TABLE
insert into MyParentTable(col1,col2,col3)
select SDATA1,SDATA2,SDATA3
from ExampleTables
I Would like to get the list of my newly entries.
How to do that?
IMPORTANT NOTE : Consider that MyParentTable can alreaydy contains data.
insert into dbo.MyParentTable(col1,col2,col3)
output inserted.identity_column_name
select SDATA1,SDATA2,SDATA3
from dbo.ExampleTables;
If there are foreign keys involved, you may have to use a #table variable for temporary holding.
DECLARE #t TABLE(id INT);
insert into dbo.MyParentTable(col1,col2,col3)
output inserted.identity_column_name INTO #t
select SDATA1,SDATA2,SDATA3
from dbo.ExampleTables;
SELECT id FROM #t;
I have a mySQL db with duplicate records, as from the attached image.
I am asking for a query to delete all duplicate records based on date + time, for all tables (foreachtables) in db
Thanks
As far I could see, you dont have autoincrement primary key or foreign key.
If you dont have tables with foreign key or relation between, first you can list all your tables. After that, you can create a temporal "mirror" of one table (for eg, autogrill).
Then you can do a:
INSERT INTO TemporalTable
SELECT DISTINCT
or a
INSERT INTO TemporalTable
SELECT Id, Date, Time FROM autogrill GROUP BY Id, Date, Time HAVING COUNT(*) > 1
.
TRUNCATE or DELETE FROM
without where and then put again your data with
INSERT INTO autogrill
SELECT * FROM TemporalTable
BE AWARE if you have primary keys doing this.
How about you create and STORED PROCEDURE for this?
DELIMITER $$
CREATE PROCEDURE `DeleteDup`()
BEGIN
-- Drops the table.
DROP TABLE bad_temp;
-- Creates a temporary table for distincts record.
CREATE TABLE bad_temp(id INT, name VARCHAR(20));
-- Selects distinct record and inserts it on the temp table
INSERT INTO bad_temp(id,name) SELECT DISTINCT id,name FROM bad_table;
-- Delete All Entries from the table which contains duplicate
-- (you can add also condition on this)
DELETE FROM bad_table;
-- Selects all records from temp table and
-- inserts back in the orginal table
INSERT INTO bad_table(id,name) SELECT id,name FROM bad_temp;
-- Drops temporary table.
DROP TABLE bad_temp;
END$$
DELIMITER ;
Please change tablename and column name to your desired schema.
so when you finish creating your STORED PROCEDURE, you can use it like this:
CALL DeleteDup();
You can export your table using this request :
SELECT * FROM autogrill GROUP BY id
Then, empty your table, and import the export you made before. I don't know another easy way to erase duplicate entries using only a single request.
One easy way to do this is to copy all the distinct records into a new table or an export. Then delete all records in the original table and copy them back in.
Export NULL if table have autoincrement an for source use alias name, example :
INSERT INTO product
SELECT NULL,p.product_sku,
p.product_s_desc,
p.product_desc,
p.product_thumb_image,
p.product_full_image,
p.product_tech_data,
p.product_publish,
p.product_weight,
p.product_weight_uom,
p.product_length,
p.product_width,
p.product_height,
p.product_lwh_uom,
p.product_url,
p.product_in_stock,
p.product_available_date,
p.product_special,
p.create_date,
p.modify_date,
p.product_name,
p.attribute
FROM product AS p WHERE p.product_id=xxx;