MySQL import column into table that matches key - mysql

I've got 2 MySQL tables that I want to merge into one. I've been searching and I couldn't found the way of doing this specific merge:
I want to import a column from table B (key, column) to a new column of table A(key, column1,column2...). This is easy. But only the values of table B that its key is equal to a key of A, and I want it to be in that row.
For example: if I had this 2 tables:
TABLE1 TABLE 2
ID NAME ID TEAM
1 "name1" 1 "bt"
2 "bt2"
I want the result to be:
TABLE1
ID NAME TEAM
1 "name1" "bt"
Thanks!

You would use update with join:
update table1 t1 join
table2 t2
on t1.id = t2.id
set t1.team = t2.team;
However, there really is no reason to do this. You seem to be starting with a normalized data structure, and this is usually a good thing. You can just join in the value wherever you need it, perhaps by creating a view (if you really want).

Related

In SQL when we join tables, is there a name for the new table?

E.g. in Pandas, we can apply a mask and create a new dataframe and assign it a name. Similarly in SQL, once I do a LEFT JOIN of 2 tables, is there a way to refer to the new combined table ?
You can join two table and can get the result in the new combined and also you can give name to that table . Just try this query and if get any doubt just feel free to ask anytime.
MYSQL QUERY
EMP(C1, C2, CD1)
DEPT(D1, D2)
SELECT NEWTABLE.First, NEWTABLE.Third
FROM
(SELECT E.C1 AS First, E.C2 AS Second, D.D2 AS Third FROM EMP E, DEPT D WHERE
E.CD1 = D.D1) NEWTABLE
WHERE NEWTABLE.Second > 20;
We have created a virtual table i.e "NEWTABLE" you can give your name also .
(SELECT E.C1 AS First, E.C2 AS Second, D.D2 AS Third FROM EMP E, DEPT D WHERE
E.CD1 = D.D1)
This is the query for where we have applied join query and also we have selected the three row from two table and renamed it as "FIRST", "SECOND" and "THIRD".
And you will get the doubt in the first line so let me clear we have performed the operation NEWTABLE.Second > 20;on the new table which we obtained after join.
If you still get any doubt regarding the query just ask .
Values Stored in the new table is temporary and you can use it for that query only.
And if you want to store permanent value then you have create to new table then assigned that table with the table we joined and so on .
No that won't work in sql, at least not directly
But you can do a subquery
Like
SELECT aa.*
FROM
(SELECT t1.*,t2.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.refid) aa
or A view
CREATE VIEW v AS SELECT t1.*,t2.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.id = t2.refid;
A problem can result, when you have in both tables the same names for columns, that would cause problems, so you must check and in case of equal columnames alias the second column

SQL Tables Joining issues

I have two tables, Table 1 and 2. I want to update information at Table 1 based on Table 2.
For example, correct AA in table 1 from 10 to 30.
What quires should I write?
Thanks,
you don't want to do a join from what I can tell, but instead you should do an update. It does get a bit more complicated when you're using data from another table instead of feeding raw data straight into the query.
UPDATE Table1 t1,
Table2 t2
SET t1.num = t2.num
WHERE t1.name == t2.name;
not the exact code of course because the question and tables are somewhat vague but I believe this is the right direction.
Try an update with a join.
UPDATE TABLE1 a
JOIN TABLE2 b
ON a.join_colA = b.join_colA
SET a.numberColumn = b.numberColumn
Here column join_colA is your first column numberColumn would be your other value column.
I would solve this problem in 3 steps
Step 1: Join the tables
Step 2: Update the null values from table 1
Step 3: Drop the unnecessary column
Select A.*, B.Column2 as column3 from A
left join B
on A.Column1=B.Column1
update table1
set column3= column2
alter table table1
drop column column2
Figure out the syntax errors that you may encounter

MySQL: deleting rows based on a condition with data from another table and NO JOIN

I have a TABLE1 like this:
And a TABLE2 like this:
I want to delete entries from table 1 whose endTimestamp is not equal to ANY table 2 entry endTimestamp, with a margin of 1000 time units.
(I know in this example all the entries from table 1 and table 2 have the same timestamp values, so the 5 entries from table 1 should be kept, and any other should be erased if existed)
Since the ids of both tables are not related to each other, I can't perform a JOIN operation as long as I know.
How can I do this?
EDIT: Tried here. Works, but does not work at my server :|
You're looking for :
delete from table1 where endTimestamp not in (Select endTimestamp from table2)
Edit : As pointed out by #user2864740, you can very well use Join here too, even if the ids of both tables are not related to each other.
DELETE FROM table1 INNER JOIN table2 ON table1.endTimestamp = table2.endTimestamp;

MYSQL request from two tables, in order to display a table

I have got a MySQL database, managed with phpMyAdmin.
I am not very clever at MySQL requests.
In table1 are (among others) the 2 following columns :
- id_product
- active
In table2 are (among others) the 2 following columns :
- id_product
- description
I would like to write a request that display a table as follows :
having, at least, the id_product column and the description column
and having only product for which the active field is equal to 1 (the active field can only have a value of 0 or 1)
Thank you in advance for any help in this matter.
Patrick
You want to make an Inner Join between the tables. This matches rows from one table to rows in another, excluding rows that don't have a match. Then you want to add a restriction to only return those with active = 1.
This looks like:
Select t2.id_product, t2.description from table2 t2
inner join table1 t1
on t1.id_product = t2.id_product
where t1.active = 1;

(Fast) searching in spite of row inheritance

I currently have a table which has these columns:
id (INT)
parent_id (INT)
col0
col1
col2
As an example there are the following entries saved in this table:
1 NULL abc def NULL
2 1 test NULL NULL
3 1 NULL NULL xyz
Now I'd like to search in all rows A which haven't got any rows B which are pointing to them (B.parent_id = A.id). In addition the row values should be either the ones that are present in the current row or if there is a NULL, the values of the parent should be considered.
To illustrate my requirements I'd like to show some examples:
SEARCH(col0=test) => #2 (#1 has some children, #3.col0 = abc (inherited from #1))
SEARCH(col1=def) => #2, #3 (#1 has some children)
SEARCH(col2=xyz) => #3 (#1 has some children, #2.col2 = NULL (inherited from #1))
Does anyone know how to implement such a search in MySQL?
SELECT
# if first table has no value, use parent table
IF(t1.col0, t1.col0, t2.col0) as virtcol0,
IF(t1.col1, t1.col1, t2.col1) as virtcol1,
IF(t1.col2, t1.col2, t2.col2) as virtcol2
FROM table as t1
LEFT JOIN table as t2 ON t1.parent_id = t2.id
LEFT JOIN table as t3 ON t1.id = t3.parent_id
# t3 would be children of t1. We don't want t1 to procreate. :)
WHERE t3.id IS NULL
# Your actual search goes here:
AND virtcol0/1/2 = whatever
Fast? No. Best index use you can get out of this are the joins on id/parent_id.
If you have a lot of data and small result sets, you can query the columns directly on an index and then run checks for parents and children in separate queries. That'd be a lot faster than running the above query on a huge table.
The cleanest solution would probably be to create a view with the parent and child columns merged:
CREATE VIEW foo
AS SELECT
c.id AS id,
COALESCE(c.col0, p.col0) AS col0,
COALESCE(c.col1, p.col1) AS col1,
COALESCE(c.col2, p.col2) AS col2
FROM table AS c
LEFT JOIN table AS p ON p.id = c.parent_id
WHERE NOT EXISTS
(SELECT * FROM table AS x WHERE c.id = x.parent_id)
Then you can write queries against this view as if it were a normal table.
However, as Mantriur notes, this will not be very efficient. If the table doesn't change very often, you could use CREATE TABLE ... SELECT instead of CREATE VIEW to create an actual table containing the merged data and create some indexes on it so that it can be efficiently queried. However, such a table won't track changes to the original table the way a view does.
In principle, you could use triggers (or application logic) to update the merged table in real time as the underlying table changes, but this can easily get complicated and prone to errors. Unfortunately, while some other RDBMSes do support materialized views, which are essentially a way to do this automatically, MySQL currently does not.
(Well, not natively, anyway. There is Flexviews, although I haven't tried it myself.)