Say I have got three tables: A, B and C.
A has primary key a
B has primary key a and also a non-primary key c.
C has primary key c.
I want to begin selecting from table A.
So I got a query like this:
Select * from A join B on A.a=B.a join C on B.c=C.c
It returns
Unknown column 'B.c' in 'on clause''
Is this impossible in mysql, joining a table on a joined table? Or am I just doing something wrong? BTW table and column names a made up.
It is possible, and your syntax should be correct.
please refer to this link for syntax:
http://dev.mysql.com/doc/refman/5.0/en/join.html
example to interesting things you can do in MySQL (from the documentation):
SELECT * FROM t1 LEFT JOIN (t2, t3, t4) ON (t2.a=t1.a AND t3.b=t1.b AND t4.c=t1.c)
Also note the error claims that column C does not exist - so you should check the structure of B..
Related
I was asked to update a SQL Server table's primary key column, the column already has values. On inner joining with few other tables, I had to update the PK column values from another table. It's failing to do so due to duplicates, inserting value 435 to 2 or more PK column.
Any suggestion how it can be done?
UPDATE t
SET t.ID = c.NewID
FROM Table1 t
INNER JOIN Table2 p ON p.ID = t.ID
LEFT OUTER JOIN Table3 c ON c.ID = t.ID
WHERE c.status = 'Y'
Let's say your PK is called A, so:
create a new column called B
update table set B = A
do the changes you need to B
go through all the tables you have FK to your table; drop them, and make sure to reflect the data changes;
drop the PK on A
recreate it on B
recreate all the FKs you dropped 3 steps ago on B
Simple :)
I'm joking, as you can see it is very complicated and error prone. You shouldn't have to worry about your PKs values - if you are there is something wrong in your design
I have 2 tables A and B. I wanted to compare table A with table B and for any mismatched record I wanted to update the entire record from table A to Table B. We have 2 primary keys and have around 40 million records.
Can we achieve this in one SQL or Script? Or can I create temporary table(Table c) where I can write table A records which are mismatching and then update Table B with temporary Table C. This I would be doing in MY SQL workbench and using mariaDB DB.
Added additional information - I have 2 primary keys and 15 columns. So, Ideally have to match 13 columns and find mismatches assuming the primary key matches in both tables.
Please assist and appreciate for any feedback.
One approach is to do an update left join of the B table to the A table, and update all columns in the former table with values from the latter. Note that the WHERE clause checks to make sure that a record in B did not match to any record in A.
UPDATE TABLE_B b
LEFT JOIN TABLE_A a
ON a.col1 = b.col1 AND
a.col2 = b.col2 -- AND all other columns
SET b.col1 = a.col1,
b.col2 = a.col2 -- AND set all other columns
WHERE a.col1 IS NULL
I have a table A, it has millions of records and it's growing. A new column needs be added to table A with indexing, but it could be headache to migrate such a large table. So table B is created at some point from table A, question is how to sync up these 2 tables efficiently?
There're multiple scenarios new records will be added to table A.
To 'Sync' up two tables without actually merging them you can create a UNION VIEW. A VIEW can be used just like a table for calculation, manipulation, data storage etc. This is assuming that both tables have the same amount of rows, if not you'll need to create primary and foreign keys.
CREATE OR REPLACE VIEW viewname AS
SELECT * FROM TABLE_A
UNION ALL
SELECT * FROM TABLE_B
Now if both tables don't share the same amount of rows you'll need at least 1 field in common between the two tables called primary and foreign keys, to join the tables using the primary and foreign keys you'll need to use a JOIN like this:
CREATE OR REPLACE VIEW viewname AS
SELECT TableA.FieldName, TableB.FieldName, TableA.FieldName
FROM TableA
LEFT JOIN TableB
ON TableA.primarykeyField = TableB.foreignkeyField
UNION ALL
SELECT TableA.FieldName, TableB.FieldName, TableA.FieldName
FROM TableA
RIGHT JOIN TableB
ON TableA.primarykeyField = TableB.foreignkeyField
It depends on what type of join you want but I think FULL JOIN will give you the best results, FULL JOIN's aren't supported in MySQL but using LEFT JOIN RIGHT JOIN and UNION ALL mimics the same results.
Or if you simply want to copy all the records from table A to table B you could use this.
INSERT INTO TableB
SELECT * FROM TableA;
Is there any way to use a create table with select statement where column names in conflict (or all) are aliased?
CREATE TABLE newTable
SELECT a.*, b.*
FROM tblA a
JOIN tblB b
ON a.id = b.cid
The issue is that tblA and tblB have a few columns with the same name, so I get a "duplicate column name" error on the create. I'm trying to avoid listing all the fields in the table, so I either need to selectively exclude some columns or apply and "auto alias" to the column names.
You can use the information_schema table to selectively exclude columns in a select statement. See the top answer here.
i m trying to import records from one table to another table.
i m having tables suppose A,B,C,D.
i m importing records from table A to table B.
Table B has two foreign keys which are primary keys in table C and D.
I m using query as below::
INSERT INTO B(userid,behaviorid,userNid,behaviorNid,timestamp)SELECT userid,behaviorid,userNid,behaviorNid,timestamp FROM A where userNid = ANY (select Nid from C);
but i m getting error as foreign key constraints fails.
How can i solve this.
Thanks in advance.
Well this should check Nid in C for every foreign key in C and append to B.
INSERT INTO B(userid,behaviorid,userNid,behaviorNid,timestamp) SELECT
userid,behaviorid,userNid,behaviorNid,timestamp FROM A LEFT JOIN C on A.foreignkey = C.Nid;
Make sure you try it and comment, if not working