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
Related
I have after insert trigger with insert query ,insert looks like
CREATE TRIGGER After_insert_stats_to_institution
AFTER INSERT
ON ABC
FOR EACH ROW
BEGIN
insert into xyz(col1,col2,col3)
( select col1,col2,col3
from tableA
join table B on .........)
UNION
( select col1,col2,col3
from tableb
join table B on .........)
ON duplicate key
update col3=exising-col3-value+(new col3 value inserted between mentioned 2 tables);
tableA and tableB having same table stricture with same condition but using for different purposes.
Could we set the value to a variable col3 on each select and use coalesce to find the correct one for the on duplicate update part?
I have two tables A and B
Table A has a list of all columns in table B
Table B has values in each column
In MS Access, is it possible to create a query linking the values in the column in Table A with the Columns in Table B?
The SELECT statement I am thinking of is something along the lines of:
SELECT A.Col1,A.Col2,B.Col1,B.Col2
FROM TableA AS A
LEFT OUTER JOIN TableB AS B
-- this is where I get stuck but in pseudo-code might be ..
ON value in A.Col3 = B.Col3
Thanks for any help on this even if it's "No, this can't be done in Access"
TableB is not normalized data structure. It has no relationship with TableA and join is not possible nor does it make sense. Do a UNION query on TableB to reorganize data to normalized structure.
I presume TableB has a unique identifier field.
SELECT ID, "SourceOfReferrals_SQ001" AS Source, SourceOfReferrals_SQ001 AS Data FROM TableB
UNION SELECT ID, "SourceOfReferrals_SQ002", SourceOfReferrals_SQ002 FROM TableB
UNION SELECT ID, "SourceOfReferrals_SQ003", SourceOfReferrals_SQ003 FROM TableB
UNION SELECT ID, "SourceOfReferrals_SQ004", SourceOfReferrals_SQ004 FROM TableB
UNION SELECT ID, "SourceOfReferrals_SQ005", SourceOfReferrals_SQ005 FROM TableB;
Now this query can be joined to TableA if there are other fields in TableA you want data from.
Alternative is VBA looping fields of recordset to write data to a normalized table.
I am trying to find all records in TableA which are not in TableB by comparing 2x columns in each table. I have tried all sorts of queries but I cannot figure it. Any help would be much appreciated.
TableA Has ColumnA and ColumnDate
TableB Has ColumnA and ColumnDate
So I only want to see which records in TableA do not match TableB for both columns.
Also, TableA may have several matching fields so I need to group them to have only 1 entry into TableB for each match.
Yes I want to insert the new records into TableB.
It appears harder than expected.
Something like this:
select ColumnDate, ColumnA
from TableA
where
(( ColumnDate not in (select ColumnDate from TableB) )
and ( ColumnA not in (select ColumnA from TableB) ))
group by ColumnA, ColumnDate;
There are two approaches to this, If I want to just sync two tables I use straight INSERT SELECT.
INSERT INTO TableB (ColumnA, ColumnDate)
SELECT FROM TableA (ColumnA, ColumnDate)
ON DUPLICATE KEY UPDATE ColumnDate = ColumnDate
But be careful, you need to have Primary Key or Unique index on TableB, so the engine will only insert new rows based on uniqueness/pk of the receiving table.
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
How do you insert into table A all the rows from table B that match a condition from table C?
For instance,
INSERT INTO tableA SELECT * FROM tableB WHERE tableB.id=tableC.id;
This does not work, because mysql does not recognize tableC.
Then add tableC after From keyword and use tableB.* instead of *
INSERT INTO tableA SELECT tableB.* FROM tableB,tableC WHERE tableB.id=tableC.id;