+ 1
How do I get company names from two different tables using SQL?
I have 3 tables. (Company,OldCompany,NewCompany) I want to show 2 columns Old Name and New Name. Company table have ID and Name OldCompany table have ID NewCompany table have ID How I get the name of the companies on two fields (Old Name, New Name) through the companies name field?
5 Antworten
+ 4
Mohamed Ashraf Mohamed
First,correct your spelling `Sql`,instead of `sal`..
Question:-
How do I get company name from two different tables?
Answer:-
You need to use a JOIN operation.
Example:
SELECT OC.Name AS Old Name, NC.Name AS New Name
FROM OldCompany OC
JOIN NewCompany NC ON OC.ID = NC.ID
JOIN Company C ON OC.ID = C.ID;
+ 3
This example shows principle mentioned by Darpan kesharwani🇮🇳 :
https://sololearn.com/compiler-playground/cZ56C94LXmpK/?ref=app
For more help will be needed your specific example of mentioned tables.
+ 3
Mohamed Ashraf Mohamed
The question is how to build a database with more tables.
If you would have only one table then this could have following colums: id, OldName, NewName. Each row get a new data set.
Some time is better to split such table. You could have for example following tables:
Company, OldCompany.
The second table must have columns id, name.
First table must have name, Oldid which id‘s equals to them of second table. In this way a data set from one table has conection with data set of second table.
In your example a column of company table mix diferent kind of data. And that cannot work.
+ 1
Darpan kesharwani🇮🇳
The OldCompany and NewCompany tables don’t have name they have just ID
0
JaScript
Bro OldCompany and NewCompany dont have Company Filed Name, Check below.
CREATE TABLE Company (CompanyID INTEGER, Name TEXT)
CREATE TABLE OldCompany (CompanyID INTEGER)
CREATE TABLE NewCompany (CompanyID INTEGER)
Company (CompanyID,Name)
Value 1, OldA
Value 5,OldB
Value 15,OldC
Value 4,NewD
Value 17,NewF
Value 10,NewM
OldCompany (CompanyID)
Value 1
Value 5
Value 15
NewCompany (CompanyID)
Value 4
Value 17
Value 10