MySQL - INSERT INTO Table With a New ID - mysql

I have two tables and I want to copy a row from one to the other. However they both use an auto incrementing ID number as the primary key which is preventing me from copying a row if the ID already exists in the other table.
What I want to do is:
INSERT INTO tableB SELECT * FROM tableA WHERE ID = 1
What I was thinking might be possible would be to store the row as a variable, temporarily set the ID to be blank, and then it will be assigned the next highest number once inserted into the new table?
Is something like this possible?
Edit:
Columns
ID, Deal_ID, Redeemed_At, Wowcher_Code, Deal_Title, Customer_Name, House_Name_Number, Address_Line_1, Address_Line_2, City, County, Postcode, Email, Phone, Date_of_Birth, Custom_Field, Marketing_Permission, Product_Name, Product_Options

You cannot use * (all columns) in this case .. you must explicitally set the columns you need eg:
INSERT INTO tableB (col1, col2, col3)
SELECT col1, col2, col3
FROM tableA
WHERE ID = 1
avoinding the ID columns

Just select the columns without the ID.
INSERT INTO tableB SELECT name,age,any_other_column FROM tableA WHERE ID = 1

Try replace asterisks with fields list withouth ID
INSERT INTO tableB SELECT Field1,Field2,Field3...FieldXX FROM tableA WHERE ID = 1

Related

Join 2 tables with all same column headers but one different

How do I join 2 tables that have all of the same column headers but one?
The other 3 columns are all the same then one table has the column header "January" and the other table has the column header "February"
So I would like to end up with 5 total columns in new table
I would also like all of the data in there even if there are duplicates. Before I used a union all but now I cannot do that because not all my headers are the same.
Thanks!
You can use UNION and rename one of your columns:
SELECT id, name, age, xxx FROM table1
UNION
SELECT id, name, age, yyy AS xxx FROM table2
If you add more information, I can elaborate my answer
select tb1.col1, tb1.col2, tb1.col3, tb2.col3 from tb1
inner join tb2 on (tb1.col1 = tb2.col1)
Assuming uniqueness when just considering the three columns, joining them
SELECT tab1.col1,
tab1.col2,
tab1.col3,
tab1.january,
tab2.february
FROM tab1,
tab2
WHERE tab1.col1 = tab2.col1
AND tab1.col2 = tab2.col2
AND tab1.col3 = tab2.col3
This assumes all rows in tblA have corresponding ones in tblB and vice versa:
SELECT * FROM tblA INNER JOIN tblB USING (col1, col2, col3);
If that is not the case, and depending on the data (if tbl1.january can have legitimate null values, this won't work), you could try something like (didn't check for sure, might need massaged with parenthesis, subselecting, etc...):
SELECT *
FROM tbl1
LEFT JOIN tbl2 USING (col1, col2, col3)
UNION ALL
SELECT * FROM tbl2 LEFT JOIN tbl1 USING (col1, col2, col3)
HAVING tbl1.`january` IS NULL
;

Compare data of two tables, store common data in a third, else in fourth table

I have four tables having same structure. Say they have a column name in them. I need to check if values in column name of table1 is present in column name of table2.
If present, I need to insert this name in column name of table3, else in column name of table4.
Assuming INSERT and Postgres ...
I would use a data-modifying CTE (Postgrs 9.1+) with one SELECT and two INSERT:
WITH cte AS (
SELECT t1.name, t2.name IS NULL AS t2_missing
FROM table1 t1
LEFT JOIN table2 USING (name)
)
, ins AS (
INSERT INTO table3 (name)
SELECT name FROM cte WHERE NOT t2_missing
)
INSERT INTO table4 (name)
SELECT name FROM cte WHERE t2_missing;
The same is not possible in MySQL (no CTEs, not to mention writable CTEs).

A way to SELECT fields from one table and INSERT to another?

I have two tables, TableA and TableB
TableA has 9 fields
TableB has 7 fields
There are 2 fields (id and name) that are identical in both tables, is there a way to select ONLY these two fields from TableA and insert them into TableB?
I have looked at the INSERT INTO... SELECT method using this statement:
INSERT INTO TableB
SELECT id, name
FROM TableA
WHERE id = 1
But I get the following error:
#1136 - Column count doesn't match value count at row 1
I assume this error is not allowing me to insert only 2 fields into the table? If so, is there a way around this or an alternative method?
Thanks
Try:
INSERT INTO TableB(id, name)
SELECT id, name FROM TableA where id = 1;
One would have to assume that the column names in TableB match TableA otherwise you would need to put in the right names.
You need to specify the column names for TableB (and possibly specify TableA.id in the WHERE clause):
INSERT INTO TableB (id, name)
SELECT (id, name)
FROM TableA
WHERE TableA.id = 1
Specify the columns in table b
INSERT INTO TableB (id, name)
SELECT id, name
FROM TableA
WHERE id = 1

MySql Duplicate-data an entry on another table

I have table1,table2 => all fields are identical except that table2 two has an extra field which is a FK of table1
**table1** *ID*,content,status
**table2** *ID*,content,status,tid
so tid = table1 id.
I need to copy a row from table1 one to table2 so esentially the table2 would be a backup of table1. I can do it using mysql,then php, then mysql again I was wondering if you could have a simpler solution on mysql :)
hope its not too complicated
If you want to copy every row in table1 into table2, you could do:
INSERT INTO table2 (id, content, status, tid)
SELECT id, content, status, id FROM b;
If table2 isn't empty, you could add an ON DUPLICATE KEY... clause to deal with clashes.
EDIT
If you just want to copy one row, you can add a WHERE clause:
INSERT INTO table2 (id, content, status, tid)
SELECT id, content, status, id FROM b WHERE id=123;
INSERT INTO table2 (content, status, tid) SELECT content, status, ID FROM table1

inserting records in a MySQL table depending on existing values in another table

I am using MySQL. I want to insert some records in a table provided that they do not
exist in another table. So I want to perform something like this:
INSERT INTO sales (name, id)
SELECT name, id FROM sales_h
WHERE name AND id NOT IN (SELECT name, id FROM T_sales);
The problem is that MySQL does not allow this kind of syntax (something to do with the where clause...) I tried using CONCAT but with no result.
Any clue??
INSERT
INTO sales (name, id)
SELECT name, id
FROM sales_h
WHERE (name, id) NOT IN
(
SELECT name, id
FROM t_sales
)
Have you tried the EXISTS syntax?
INSERT INTO sales (name, id)
SELECT name, id
FROM sales_h
WHERE NOT EXISTS (SELECT * FROM T_sales WHERE T_sales.name = sales_h.name AND T_sales.id = sales_h.id);
MySQL allows you to use a tuple like a variable:
This is your statement:
INSERT INTO sales (name, id)
SELECT name, id FROM sales_h
WHERE name AND id NOT IN (SELECT name, id FROM T_sales);
The problem is "name AND Id". Turn that into a tuple:
INSERT INTO sales (name, id)
SELECT name, id FROM sales_h
WHERE (name, id) NOT IN (SELECT name, id FROM T_sales);
Personally, I don't like this much for two reasons: the tuple as variable doesn't work (or works differently) on other RDBMSes, and IN tends to perform poorly in many situations, so I like to make it a habit not to use IN.
As jhonny-d-cano-leftware states (I've upmodded him), I'd use a where not exists:
INSERT INTO sales (name, id)
SELECT name, id FROM sales_h a
WHERE not exists (
SELECT *
FROM T_sales b
where b.id = a.id and b.name = a.name);
You might look into the ON DUPLICATE clause for MySql
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
You can't say WHERE name AND id NOT IN... you need a separate clause for each. But do you have id as the primary key in table T_sales? If so, then that's all you need to check for, sp something like:
INSERT INTO sales (name, id)
(SELECT name, id FROM sales_h WHERE id NOT IN (SELECT id FROM T_sales));
EDIT
Hmmm from other people's answers it might be best to ignore me as they seem to know more :-)
The problem is your WHERE.
It's not reading as you are. You're reading it as "WHERE (name and id) NOT IN (...)", whereas it's reading it as "WHERE name (isn't null) AND (id NOT IN (...))"... if you get what I mean.
Change to this: ROW(name, id) NOT IN (SELECT name, id FROM T_sales);
INSERT INTO table_name(column1, column2) SELECT "value one", "value two" FROM DUAL
WHERE NOT EXISTS(
SELECT column1 FROM table_name
WHERE column1 = "value one"
AND column2 = "value two" LIMIT 1
)