To join 2 tables with specific columns and to create new column - join

I have created 2 tables in mysql
Table1 (sno, name, student id)
Table2 (sub id, sub code)
I need the another table as
Table3 (sub id, student id, mark)
How to join table1 and table2 to get the columns sub id and student id and to create new column mark to the table (table3).
I have created table 1 and 2 . Struggling to create table 3 .

Related

Is it possible to join a column from Table B to Table A without creating a new table C in Hive SQL?

I'm working with very large tables in Hive so I'd like to avoid having to create a whole new table when joining a single column from Table 2 to Table 1.
My first pass using the INSERT and UPDATE statements with the following test data didn't work.
Is there a way to do this or is it simpler to just create Table 3 by joining Table 1 to Table 2 and then dropping Table 1?
DROP TABLE IF EXISTS table_1;
CREATE TABLE table_1 (id VARCHAR(64), cost INT, diag_cd VARCHAR(64));
INSERT INTO TABLE table_1
VALUES ('A0001', 1000, 'A1'), ('A0001', 2000, 'B1'), ('A0001', 3000, 'B1'),
('B0001', 5000, 'A1'), ('B0001', 10000, 'B1'), ('B0001', 15000, 'C1'),
('C0001', 11000, 'B1'), ('C0001', 14000, 'C1'), ('C0001', 20000, 'C1');
DROP TABLE IF EXISTS table_2;
CREATE TABLE table_2 (id VARCHAR(64), prodt_cd VARCHAR(64));
INSERT INTO TABLE table_2
VALUES ('A0001', 'OAP'), ('B0001', 'OAPIN'), ('C0001', 'MOAPIN');
INSERT INTO TABLE table_1 prodt_cd VARCHAR(64);
UPDATE table_1 t1 SET t1.prodt_cd = t2.prodt_cd
INNER JOIN table_2 t2
ON t1.id = t2.id;
After some research and help from Mike67 I found a solution.
It appears Hive does not support COLUMN UPDATE or MERGE statements, but a simple alternative is to create an empty table and then populate it with fields from a join:
DROP TABLE IF EXISTS table_3;
CREATE TABLE table_3 LIKE table_1;
INSERT INTO TABLE table_3
SELECT a.*, b.prodt_cd
FROM table_1 AS a
LEFT OUTER JOIN table_2 AS b
ON a.id = b.id;

Join tables in Hive using LIKE

I am joining tbl_A to tbl_B, on column CustomerID in tbl_A to column Output in tbl_B which contains customer ID. However, tbl_B has all other information in related rows that I do not want to lose when joining. I tried to join using like, but I lost rows that did not contain customer ID in the output column.
Here is my join query in Hive:
select a.*, b.Output from tbl_A a
left join tbl_B b
On b.Output like concat('%', a.CustomerID, '%')
However, I lose other rows from output.
You could also achieve the objective by a simple hive query like this :)
select a.*, b.Output
from tbl_A a, tbl_B b
where b.Output like concat('%', a.CustomerID, '%')
I would suggest first extract all ID's from free floating field which in your case is 'Output' column in table B into a separate table. Then join this table with ID's to Table B again to populate in each row the ID and then this second joined table which is table B with ID's to table A.
Hope this helps.

based on the table value i need to joining a table in linq

I having the three tables
Table1
1.id
2.name
3.address
4.City
5.InstituteName
Table2
1.collegeId
2.CollegeName
3.CollegeAddress
Table3
1.Schoolid
2.SchoolName
3.SchoolAddress
Now i need to show view in html table (MVC 2) in table1 the instituteName is contains a flag if its 0 then its school(table3) if its 1 then College(table2) now my view is
1. id
2. Name
3. SchoolName (OR) CollegeName
4. School (OR) CollegeAddress
how i can do this friends please help me in linq

Join multiple FK values in one table

I'm writing some access queries and I'd like some help on a particular query. I'm still very new to SQL. Here is a simplified version of my tables:
Project Details
---------------
projectID (PK)
projectStartDate
projectEndDate
projectName
managerID (FK)
leadID (FK)
coleadID (FK)
Employee
--------
empID (PK)
empName
The managerID, leadID, and coleadID all correspond to an empID. I'd like to retrieve the Project Details table, but replace the IDs with the names of the employees. I've successfully been able to do this for one FK at a time using an inner join, but can't figure out a way to accomplish this for all roles.
It would also be nice to be able to change the attribute names on the results to managerName, leadName, and coleadName.
Thanks!
It's the same way, you probably have done it for one ID:
SELECT pd.*
, emp_m.empName ManagerName
, emp_l.empName LeadName
, emp_c.empName ColeadName
FROM ProjectDetails pd
, Employee emp_m
, Employee emp_l
, Employee emp_c
WHERE pd.managerID = emp_m.empID(+)
AND pd.leadID = emp_l.empID(+)
AND pd.coleadID = emp_c.empID(+)
The (+) is for an outer join, so it will select all records of the ProjectDetails table, no matter if it can match the manager, lead or colead.

Linq query for joining three tables

I have three tables table1 (main table), table2, table3.
table1 contains table1Id
table2 and table3 contain table2Id, table2RoleId, table3Id, table3RoleId.
Also the same value of table1Id, more than one record in table2Id and table3Id but the table2RoleId's and table3RoleId's are different.
I want to join table1 with table2 and table3 to display like
Table2RoleId and Table3RoleId has to display according to the Table1Id
How can I achieve this?
Thanks
i'm ignore the content of your question and will show you sample left join in linq
var result = from x in table1 join y in table2
on x.tableId1 equals y.tableId1
join z in table3 on x.tableId1 equals z.tableId1
Select new {// your return fields}

Resources