I have two MySql Databases.
One has:
___id___|___name___|__date____|
Second database has
___id___|___tag___|
One common thing in this tables - id. It's same in both tables. How can i append column "tag" to first mysql database?
If you just want a view of the columns in the first table along with a possible matching tag, then use a query:
SELECT
t1.id, t1.name, t1.date, t2.tag
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id;
If you instead want to actually add a new tag column to the first table, then add that column and then do an update:
ALTER TABLE table1 ADD COLUMN tag VARCHAR(55);
UPDATE table1 t1
INNER JOIN table2 t2
ON t1.id = t2.id
SET t1.tag = t2.tag;
Related
Hi I have two table one like this one:
table1
and one like this:
table2
I would like to update all the fields on the table2 column "newID" based on this rules: if (table2.ID = table1.ID_actual or table2.ID=table1.ID_old) then table2.newID = table1.newID
How can I resolve this problem ?
You need a join of the 2 tables in the UPDATE statement:
UPDATE table2 t2
INNER JOIN table1 t1 ON t2.ID IN (t1.ID_actual, t1.ID_old)
SET t2.newID = t1.newID
Im trying to create a view where specific Columns from 2 tables to be combined.
My Tables
Table1(Column1,Column2,Column3)
Table2(Column4,Column5,Column6)
Expected output
View1(Column2,Column3,Column6)
What query can i use to achieve that output?
CREATE VIEW [dbo].[View]
AS
SELECT a.Column2,a.Column3,b.Column6
FROM Table1 AS a
INNER JOIN Table2 AS b ON A.ID = B.ID -- your logic
If you have and id to join tables:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
join table2 t2 on t1.id = t2.id
If you don't have and id and want to get mixes columns without relationships:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
cross join table2 t2 on t1.id = t2.id
As Fourat and jarhl commented above, you need to explicitly define what columns are used to JOIN both tables. So using the below code change the ID column to the column that relates these two tables, and if it's more than one column the list them all separated by AND.
CREATE VIEW dbo.YourViewName
AS
SELECT t1.Column2, t1.Column3, t2.Column6
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
GO
I have two tables t1, t2 that I have created and loaded data from a CSV into these.
I had to then create a new PK column as the existing columns (t1.old_id, t2.old_id) are strings that would naturally be a PK are not absolutely fixed (this seems to be advised against?)
so I created a id PK INT AUTO_INCREMENT in each table
as one record in t1 is linked to many in t2 and I want to maintain referential integrity between these two tables.
I believe what i need to do is create an id INT NOT NULL in t2 as an FK
This t2.id is blank at the moment (as it is dependent ont1.id`)
Am I right in thinking I need an UPDATE query with a JOIN of some description to make this work?
The following produces the data exactly that I want to update into my t2.id column - but I don't know how to do the update
select t1.id
from t1
inner join t2
on t1.old_id = t2.old_id
You can use a join in your UPDATE statement like this:
UPDATE t2
JOIN t1 ON t1.old_id = t2.old_id
SET t2.id = t1.id
You can use a correlated UPDATE query like this
UPDATE t2
SET id = (SELECT MAX(t1.id) FROM t1 WHERE t1.old_id = t2.old_id);
*Assuming you have a single t1.id for each t1.old_id
On a Separate Note, You should name t2.id like t2.t1ID so as to remove ambiguity if and when you have a identity column in t2 as well named id
I am using mySQL 5.6. I have two tables: t1 and t2. Both have many columns. And many columns in t1 and t2 share the same name: for example, there is a "var1" column in t1 and in t2.
I want to join the tables, selecting (a) all columns from t1 and (b) only the columns in t2 that have names that don't appear in t1. For example, I would not select "var1" from t2.
Here is a valid mySQL command that does not work because some columns share the same name:
SELECT * FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
MySQL sensibly returns this error message:
ERROR 1060 (42S21): Duplicate column name 'var1'
So I want to run a command like
SELECT t1.*, DISTINCTCOLUMNS(t2.*) FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
Except, of course, that there is no DISTINCTCOLUMNS operation in SQL. But is there a similar (real) command that will achieve the same effect?
I see that common advice is to avoid SELECT * syntax, partly for efficiency purposes. I appreciate that, but I do not want to write out the names of all of the columns that I need.
The real issue here is you have the same column name for both tables. So you need to alias your columns.. This is also why you shouldn't just pull all columns out but the specific ones you need..
SELECT t1.ID as t1_id,
t1.var1 as t1_var1,
t2.ID as t2_id,
t2.var1 as t2_var1,
... Etc.
FROM t1
LEFT JOIN t2 on t1.ID = t2.ID
With two ID columns and no way to distinguish between the two an error will occur
You could qualify the columns like
SELECT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
But you should not do it unless the columns with identical names do actually hold/reference the same data.
try like following. this may help you
SELECT t1.*, t2.YourDesiredColumnName FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
I have a simple task in MySQL. I have two tables t1 and t2. Table t1 has one column in common with table t2.
What I want is to return all the columns from both tables (order is not important) but making sure that the rows of both tables are matching based on a common field, say fieldx. However, not all the rows in table t1 have a matching row in table t2 (i.e. table t1 has much more rows, but all rows in t2 have a matching row in t1 ).
I tried to use where as such:
SELECT t1.* t2.* from t1, t2 where t1.fieldx=t2fieldx
However this returns only the number of rows as there are in table t2.
I also tried right and left joins, but couldn't get that to work either (this way not all columns of both tables were included..)
How would I do this?
The LEFT JOIN will give you all the rows from t1 and if t2 does not have an entry for it, t2 columns will appear as NULL:
SELECT t1.*,
t2.*
FROM t1
LEFT JOIN t2
ON t1.fieldx = t2.fieldx
Here is an Live DEMO of the above said, as you can see in t2, I've only referenced the first 3 records of t1 hence why the rest of the t2 information comes out NULL, as there is no t2 record to that given t1 entry.
I want is to return all the columns from both tables (order is not
important) but making sure that the rows of both tables are matching
based on a common field, say fieldx.
This query solve this:
SELECT t1.* t2.* FROM t1 INNER JOIN t2 WHERE t1.fieldx = t2fieldx
Further, if the next phrase is true, you can try with LEFT OUTER JOIN too (or RIGHT).
but all rows in t2 have a matching row in t1
SELECT t1.* t2.* FROM t2 LEFT OUTER JOIN t1 WHERE t1.fieldx = t2fieldx