Solved SQL to append new columns to table

jaryszek

Registered User.
Local time
Yesterday, 16:22
Joined
Aug 25, 2016
Messages
756
Hi Guys,

i have 2 tables:
Table1

IDPetKindColor
1​
CatMammalBlack
2​
DogMammalWhite
and

Table2 Table2

IDFood
1​
Fish
2​
Meet

What i want to do is to get result like here:
ExpectedResult

IDPetKindColorFood
1​
CatMammalBlackFish
4​
CatMammalBlackMeet
5​
DogMammalWhiteFish
6​
DogMammalWhiteMeet
so as you can see for Eech Per and Kind column (co Mammal and Dog and Cat) i just appended one new column = Food.

How can i write SQL to do it?
Best,
Jacek
 

Attachments

You don't need cat ID 1 & cat ID 4, or Dog ID 5 & Dog ID 6.

You only need one ID for each animal:-

Cat ID 1, Dog ID 5



You need an extra table:-

PetAndFoodJoin

Pet ID --- FoodID
----1------------ 1
----1------------ 2
--- 5 ----------- 1
--- 5 ----------- 2

From this arrangement you can get the results you desire ..
 
Hmm and it is not possible to do this without additional tables only with SQL?

Thanks!

Best,
Jacek
 
Hmm and it is not possible to do this without additional tables only with SQL?

I'm sure that it is possible. There are some very clever people here, who will write code that can do just about anything.

But if you go with the wisdom of the experienced database developers on this site, you will find that the approach I advise has many advantages..

Anything else you do is likely to become a nightmare very quickly!
 
Thank you,

let's wait i want to know it :) And will decide.

Best,
Jacek
 
A Cartesian query can produce the output you show (except for the ID field). What purpose do you want this for?

SELECT Table1.*, Table2.Food FROM Table1, Table2;


BTW, correct spelling: "meat", not "meet".
 
Thank you Guys,

i needed cross join here,

Best,
Jacek
 
I can't make sense of the tables or the question. Cats and dogs are both carnivores and so would eat similar foods But what if you had other animal types? Birds and Fish don't eat the same foods as cats and dogs and neither to cows.
 

Users who are viewing this thread

Back
Top Bottom