How to split data from two columns in one table into multiple columns of the result table
So I am trying to get mySQL query to work for a large database that I have. I have (say) two tables Table_One and Table_Two. Table_One has three columns Type, Anima and TestID and Table_Two has 2 columns Test_Name and Test_ID. An example with the values โโbelow:
TABLE_ONE
Type Animal TestID
-----------------------------------------
Mammal Goat 1
Fish Cod 1
Bird Chicken 1
Reptile Snake 1
Bird Crow 2
Mammal Cow 2
Bird Ostrich 3
Table_Two
Test_name TestID
-------------------------
Test_1 1
Test_1 1
Test_1 1
Test_1 1
Test_2 2
Test_2 2
Test_3 3
In Table_One, all types fall under one column, and values โโof all types (mammals, fish, birds, reptiles) fall under another column (Animals). Table_One and Two can be related Test_ID
I am trying to create a table like below:
Test_Name Bird Reptile Mammal Fish
-----------------------------------------------------------------
Test_1 Chicken Snake Goat Cod
Test_2 Crow Cow
Test_3 Ostrich
This should be my final table. The approach I am currently using is to create multiple instances of Table_One and use joins to form this resulting table. So the column "Bird", "Reptile", "Mammals" and "Fish" are all taken from another copy of Table_one.
For example,
Select
Test_Name As 'Test Case Name',
Table_Bird.Animal AS 'Birds',
Table_Mammal.Animal AS 'Mammal',
Table_Reptile.Animal AS 'Reptile,
Table_Fish.Animal AS 'Fish'
From Table_One
INNER JOIN TABLE_Two AS Table_Bird
On Table_One.TestID = Table_Bird.TestID
INNER JOIN TABLE_Two AS Table_Mammal
On Table_One.TestID = Table_Mammal.TestID
INNER JOIN TABLE_Two AS Table_Reptile
On Table_One.TestID = Table_Reptile.TestID
INNER JOIN TABLE_Two AS Table_Fish
On Table_One.TestID = Table_Fish.TestID
Where Table_Bird.Type LIKE 'Birds'
AND Table_Mammal.Type LIKE 'Mammals'
AND Table_Reptile.Type LIKE 'Reptiles'
AND Table_Fish.Type LIKE 'Fish'
The only problem with this query is that all records for birds, mammals, reptiles and fish have some value. If one field is empty, as for Test_Two or Test_Three, it does not return that record. I used Or instead of A in the WHERE clause, but it didn't work.
I would really appreciate if someone can point me in the right direction.
source to share
You can try and take you there to the JOIN state and make it LEFT JOIN instead of INNER JOIN - Like this:
Select
Test_Name As 'Test Case Name',
Table_Bird.Animal AS 'Birds',
Table_Mammal.Animal AS 'Mammal',
Table_Reptile.Animal AS 'Reptile,
Table_Fish.Animal AS 'Fish'
From Table_One
LEFT JOIN TABLE_Two AS Table_Bird ON Table_One.TestID = Table_Bird.TestID AND Table_Bird.Type LIKE 'Birds'
LEFT JOIN TABLE_Two AS Table_Mammal ON Table_One.TestID = Table_Mammal.TestID AND Table_Mammal.Type LIKE 'Mammals'
LEFT JOIN TABLE_Two AS Table_Reptile ON Table_One.TestID = Table_Reptile.TestID AND Table_Reptile.Type LIKE 'Reptiles'
LEFT JOIN TABLE_Two AS Table_Fish ON Table_One.TestID = Table_Fish.TestID AND Table_Fish.Type LIKE 'Fish'
source to share