Replicate columns of a table in two other tables - sql

I have a table A, which has the columns:
cat
rat
dog
pig
I need to paste/replicate this columns in table B and table C.
But I don't want to create two INSERT INTO queries.
I saw using INNER JOIN to join two tables in one, but not the other way (that I want).
Is there any way to do it?

Related

Can I 'link' two set of data in SQL and retrieve values against both to compare?

Goal: to compare the string in a field (Dispatcher) in Table C1+2 against two strings in Table A (DF code and MD code).
Table A: Contains DFCode and MDCode (this table only exists to link these two strings together)
Table B: Contains All codes (MD and DF types) and an ItemID for each code
TableC1+2: Contains Itemcode and Dispatcher for each code.
Novice here. Table C contains all the dispatchers against an ItemID, Table B contains all Codes and ItemIDs. I want to see the two codes with their dispatchers side by side. It seems simple but I can't figure out how to look up the MDCode in table B and link it through to the dispatcher in TableC
select
DF Code,
MD Code,
DF Dispatcher,
MD Dispatcher
from TableA
LEFT JOIN TableC1
LEFT JOIN TableC2
LEFT JOIN TableB
Once I have the above cracked I'll add a "Where DFDispatcher and MDDispatcher do not match". I expect to see four columns of results showing the DFCode, the MDCode and the two Dispatcher fields which do not match.
Expected result:
DFCode|MDCode|DFDispatcher|MDDispatcher

Yii2 : Joining child table to two parent tables

I have two parent tables and 1 child table in this format -
A - a_id, a_name (parent)
B - b_id, b_name (parent)
C - c_id, a_id, b_id
aid and bid are foreign keys in table C.
I am trying to create a dataProvider starting from Model A, and want to join C to the picture such that the code looks like
$query->joinWith([B,A&B.C]
But I don't know how to make C join to both A and B for the given query using Yii2 standards. Any help would be appreciated.
Thanks
To make JOIN with Yii2 through multiple DataProvider models you can use simple:
$results = $tableADataProvider->joinWith(['cRelation.bRelation')
->where([...]);
And of course in A table DataProvider you need to have relation to C table, in this example cRelation and in C table DataProvider you need to have relation to B table bRelation.
Also to mention IF you don't use any column from tables B and C in WHERE clause, but just you want to retrieve results from B then it's much better to use $tableADataProvider->with([...]) which fetches data trough another query. This way even if they are pivot tables, you will get correct count of Table A.
Worthy to note that you probably didn't get correct count (count that you wanted) if table(s) is/are pivot table(s).
(Pivot table is a table that have more than 1 record that have same unique key of other table)

One to Many join in access

I have tow tables, one collar table with drillhole unique numbers with their x,y,z values and second table with drillhole numbers with geology information and one drillhole can have many geology information based on drillhole depth. I want to join two table. Would you please how can I join these two table in ms access. I want all the data from drillhole collar table and only matching data from geology table. I am trying but could not succeed. Please advise.enter image description here
Two Tables
If you want all data from a master table (drillhole collar table) plus matching data from a child table (geology table), this is achieved by using a LEFT JOIN query. Adapt this example by replacing the table and column names with ones that you need from your database:
SELECT d.*, g.*
FROM Drillhole d
LEFT JOIN Geology g ON d.Hole_ID = g.Hole_ID

Joining two tables to update results in third table using like

I am trying to join two tables and update the results in the third table.
So table A is the results table and it has the columns customer number and score.
Table B has customer number and ind_code and table C has ind_code and ind_score.
So the output of the query should be such that the ind_code in table B and C should join together based on the first two digits and ind_score should be updated in Table A in the score column. Table A and Table B should be joined on the basis of customer number.
Could anyone please help. I tried multiple queries but nothing seems to work. i am using oracle sql developer
Generally, the JOIN operation mustn't cut field information but if your structure (for me not correct) is that ...
If I understand better:
UPDATE TableA
SET score =
(SELECT MAX(C.ind_score)
FROM TableC C
JOIN TableB B
ON C.ind_code = SUBSTRING(B.ind_code, 1, 2)
WHERE B.customernumber = TableA.customernumber)
I use on subquery MAX aggregate function, because I don't know if your cut of ind_code of TableB can be not unique (i.e. you have ind_code 5555 and 5554)

Joining, but not joining (hypothetical q.)

Lets suppose that I have a table A with couple of columns. I work with tables, where there is no index on the entries, since they are 'historical' tables. I use one specific column, though, to sort of identify my things. Lets call this ID.
If you'd make a query like the one below, sometimes you'd get one line back, other cases a few.
SELECT * FROM A WHERE ID = '<something>'
Lets say I have two more tables, B and C. Both have ID columns, like A.
Also, some of the IDs in A, are also in B OR C. IDs existing in B CANNOT exist in C. And ALL IDs in A EXIST in either B OR C.
B and C contain extra information, which I'd like to join to A at the same SELECT.
My problem is, that they would only provide extra information. I do not want extra lines in my output..
To make it more clear: my selection from A returns a hundred lines of output.
When I left/right/inner join B table, I —probably— will have less lines as output. Same thing with joining C.
AND FINALLY, my question is:
Is there a way to join table B only on those IDs, which exist in B and vice versa? (And it I would want it in the same SELECT.... statement.)
If you don't want extra lines in your output, you could do something like this:
select *
from A join
(select B.*
from B
group by B.id
) B
on A.id = B.id;
This would choose arbitrary values from B for each id and join them back to A. Is this what you want?
Well it seems like you should build some left join between A and two "Select MAX"s: one from table B, the other one from table C.
And if you do not want 'duplicate' IDs from table A, a 'group by' on table A should help.