How to join more than 5 tables in SQL? - mysql

I want to join multiple tables with SQL.
Here is my table structure:
table 1: akdhis_mahasiswamagister (ID, MahasiswaID, NIM, MayorID, TahunMasuk)
table 2: akdmst_mayor(ID, DepartemenID)
table 3: ipbmst_departemen(ID, FakultasID, DepartmenName)
table 4: ipbmst_fakultas(ID, FacultyName)
table 5: ipbmst_orang(ID, Name, NIMS2Key)
table 6: akdhis_kelanjutanstudi(ID, NIM, IPK)
I want to create "a view table" by joining with this query:
CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost` SQL SECURITY DEFINER VIEW `aIPK` AS select
`ipbmst_fakultas`.`Kode` AS `Fakultas`,
`ipbmst_departemen`.`Kode` AS `Departemen`,
`akdmst_mahasiswamagister`.`NIM` AS `NIM`,
`akdmst_mahasiswamagister`.`TahunMasuk` AS `TahunMasuk`,
`akdhis_kelanjutanstudi`.`IPK` AS `IPK`
from (((((`akdmst_mahasiswamagister` left join `akdmst_mayor` on((`akdmst_mahasiswamagister`.`MayorID` = `akdmst_mayor`.`ID`)))
left join `ipbmst_departemen` on((`akdmst_mayor`.`DepartemenID` = `ipbmst_departemen`.`ID`)))
left join `ipbmst_fakultas` on((`ipbmst_departemen`.`FakultasID` = `ipbmst_fakultas`.`ID`)))
left join `ipbmst_orang` on((`akdmst_mahasiswamagister`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
left join `akdhis_kelanjutanstudi` on((`akdhis_kelanjutanstudi`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
WHERE IPK IS NOT NULL
order by NIM
LIMIT 100;
but, when I tried to run it, the result is empty result. What should I do to solve that problem? Thankyou

Related

Delete data from table without creating temporary table

I have three tables, that match the following diagram:
And I need to delete some data from join_table, where a label column(table_right) and name column(table_left) match some criteria.
My solution is to use a temporary table:
create temporary table if not exists data_for_deletion
(select jt.id
from join_table jt
left join table_left tableLeft on jt.table_left_id = tableLeft.id
left join table_right tableRight on jt.table_right_id = tableRight.id
where tableLeft.name = 'name' and tableRight.label = 'label');
delete from join_table where id in (select id from data_for_deletion);
My question is: is there any other way to do such deletion without creating a temporary table?
You should be able to use MySQL's multi-table DELETE syntax:
DELETE jt
FROM join_table jt
JOIN table_left tl ON jt.table_left_id = tl.id
JOIN table_right tr ON jt.table_right_id = tr.id
WHERE tl.name = 'name' AND tr.label = 'label'
Note that since you have a WHERE clause which is dependent on columns in table_left and table_right there is no point in using a LEFT JOIN as it will be converted to an INNER JOIN anyway (see the manual).

MySQL query for not exists in another table

I have two MySQL Tables...
1. master_fee
2. fees_receiving_ledger
I do not want to show the FeeId containing FeeFrequencyId : 4 from master_fee table if they are present in fees_receiving_ledger table.
Like above I do not want to show the Admission Fee (FeeId:1) containing FrequencyId : 4 as it is present in fees_receiving_ledger.
I have tried like below...
Select
master_fee.*
From
master_fee
Where Not Exists(Select
fees_receiving_ledger.FeeId
From
fees_receiving_ledger
Where
fees_receiving_ledger.FrequencyId = '4')
My query giving me blank result.
I tried This but failed.
What should be the query ?
I am using VB.NET with MySQL database.
you forgot the join condition in your subselect.
select m.* from master_fee m
where not exists (
select 1 from
fees_receiving_ledger f
where f.frequencyID = 4
and m.feeid=f.feeid)
You could also do:
select m.* from master_fee m left join fees_receiving_ledger f
on f.feeid = m.feeID
where f.frequencyID <> 4;

How to join table in SQL correctly? [duplicate]

I am really confused with this codes. I have query like this
CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost` SQL SECURITY DEFINER VIEW `aIPK` AS select
`ipbmst_fakultas`.`Kode` AS `Fakultas`,
`ipbmst_departemen`.`Kode` AS `Departemen`,
`akdmst_mahasiswamagister`.`NIM` AS `NIM`,
`akdmst_mahasiswamagister`.`TahunMasuk` AS `TahunMasuk`,
`akdhis_kelanjutanstudi`.`IPK` AS `IPK`
from (((((`akdmst_mahasiswamagister` left join `akdmst_mayor` on((`akdmst_mahasiswamagister`.`MayorID` = `akdmst_mayor`.`ID`)))
left join `ipbmst_departemen` on((`akdmst_mayor`.`DepartemenID` = `ipbmst_departemen`.`ID`)))
left join `ipbmst_fakultas` on((`ipbmst_departemen`.`FakultasID` = `ipbmst_fakultas`.`ID`)))
left join `ipbmst_orang` on((`akdmst_mahasiswamagister`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
left join `akdhis_kelanjutanstudi` on((`akdhis_kelanjutanstudi`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
WHERE `akdhis_kelanjutanstudi`.`IPK` IS NOT NULL
order by NIM
LIMIT 100;
but "IPK" result is NULL, actually IPK has its value. What's wrong with those codes?
Here is my table structure:
table 1: akdmst_mahasiswamagister (ID, MahasiswaID, NIM, MayorID, TahunMasuk)
table 2: akdmst_mayor(ID, DepartemenID)
table 3: ipbmst_departemen(ID, FakultasID, DepartmenName)
table 4: ipbmst_fakultas(ID, FacultyName)
table 5: ipbmst_orang(ID, Name, NIMS2Key)
table 6: akdhis_kelanjutanstudi(ID, NIM, IPK)
I also have the other problem about this query. It took too much time to query the view. I thought maybe it's because table akdhis_kelanjutanstudi that consists of more than 300K data records. I have used "LIMIT 100" but still the same. could you please help me to solve that problem?
I don't know what the exact problem(s) is, but your WHERE clause has a problem:
WHERE IPK IS NOT NULL
It is not allowed to refer to a column alias in the WHERE clause, because its value may not be determined yet. Instead, you should use this:
WHERE akdhis_kelanjutanstudi.IPK IS NOT NULL
Update:
The parentheses you used in your original view look strange, unnecessary, and possibly wrong. Try using the following:
CREATE ALGORITHM=UNDEFINED DEFINER=`root`#`localhost`
SQL SECURITY DEFINER VIEW aIPK AS
SELECT t4.Kode AS Fakultas,
t3.Kode AS Departemen,
t1.NIM AS NIM,
t1.TahunMasuk AS TahunMasuk,
t6.IPK AS IPK
FROM akdmst_mahasiswamagister t1
LEFT JOIN akdmst_mayor t2
ON t1.MayorID = t2.ID
LEFT JOIN ipbmst_departemen t3
ON t2.DepartemenID = t3.ID
LEFT JOIN ipbmst_fakultason t4
ON t3.FakultasID = t4.ID
LEFT JOIN ipbmst_orang t5
ON t1.NIM = t5.NIMS2Key
LEFT JOIN akdhis_kelanjutanstudi t6
ON t6.NIM = t5.NIMS2Key
WHERE t6.IPK IS NOT NULL
ORDER BY NIM
LIMIT 100;
Running sql instead of creating algorithm is easier for testing.
In your case check the conditions in join
on((`akdhis_kelanjutanstudi`.`NIM` = `ipbmst_orang`.`NIMS2Key`)))
also you need to change
WHERE `akdhis_kelanjutanstudi`.`IPK` IS NOT NULL
To
WHERE `IPK` IS NOT NULL
you have mistake in your description,
your tables are
table 1: akdhis_mahasiswamagister (ID, MahasiswaID, NIM, MayorID, TahunMasuk)
table 2: akdmst_mayor(ID, DepartemenID)
table 3: ipbmst_departemen(ID, FakultasID, DepartmenName)
table 4: ipbmst_fakultas(ID, FacultyName)
table 5: ipbmst_orang(ID, Name, NIMS2Key)
table 6: akdhis_kelanjutanstudi(ID, NIM, IPK)
but i dont see "akdhis_mahasiswamagister" table in your select
CREATE ALGORITHM=UNDEFINED DEFINER=root#localhost SQL SECURITY DEFINER VIEW aIPK AS select
ipbmst_fakultas.Kode AS Fakultas,
ipbmst_departemen.Kode AS Departemen,
akdmst_mahasiswamagister.NIM AS NIM,
akdmst_mahasiswamagister.TahunMasuk AS TahunMasuk,
akdhis_kelanjutanstudi.IPK AS IPK
from (((((akdmst_mahasiswamagister inner join akdmst_mayor on((akdmst_mahasiswamagister.MayorID = akdmst_mayor.ID)))
inner join ipbmst_departemen on((akdmst_mayor.DepartemenID = ipbmst_departemen.ID)))
inner join ipbmst_fakultas on((ipbmst_departemen.FakultasID = ipbmst_fakultas.ID)))
inner join ipbmst_orang on((akdmst_mahasiswamagister.NIM = ipbmst_orang.NIMS2Key)))
inner join akdhis_kelanjutanstudi on((akdhis_kelanjutanstudi.NIM = ipbmst_orang.NIMS2Key)))
WHERE IPK IS NOT NULL
order by NIM
LIMIT 100;
akdhis_mahasiswamagister and akdmst_mahasiswamagister are different? may you are using wrong table, and so you have incorrect information

Mysql looping through temporary table

I am trying to set a query result to a variable and then use that result in another query.
So right now I have several type of Whiskeys in my ingredients table and I can find all of them and with:
CREATE TEMPORARY TABLE TempTable (Ingredient_Id int);
Insert into TempTable Select Ingredient_Id from ingredients where INSTR(Ingredient_Name,'Whiskey')>0;
Which gives me all my id's that I need. Then I am trying to get the relevant data with:
select drink_names.*,ingredients.*, drink_recipes.*
from drink_recipes
Left JOIN drink_names ON drink_recipes.Drink_Id = drink_names.Drink_Id
Left JOIN ingredients ON ingredients.Ingredient_Id = drink_recipes.Ingredient_Id
where drink_recipes.Ingredient_Id in TempTable #This is the problem
Order By Drink_Name
I am at a loss of how to run this query with each id in the TempTable
select drink_names.*,ingredients.*, drink_recipes.*
from drink_recipes
Left JOIN drink_names ON drink_recipes.Drink_Id = drink_names.Drink_Id
Left JOIN ingredients ON ingredients.Ingredient_Id = drink_recipes.Ingredient_Id
where drink_recipes.Ingredient_Id in
(
Select Ingredient_Id
from ingredients
where INSTR(Ingredient_Name,'Whiskey')>0
)
Order By Drink_Name
or you can also try
select drink_names.*,ingredients.*, drink_recipes.*
from drink_recipes
Left JOIN drink_names ON drink_recipes.Drink_Id = drink_names.Drink_Id
Left JOIN ingredients ON ingredients.Ingredient_Id = drink_recipes.Ingredient_Id
AND INSTR(ingredients.Ingredient_Name,'Whiskey') > 0
Order By Drink_Name

How to Join 3 Tables in MySQL?

I am creating a view for my Database , I am joing 3 tables, Users,personal_info and contact_info, if you notice I have a lot of column names in my Select statement , since i don't want to include primary keys but it seems I have an error here, take a look
CREATE VIEW `payroll`.`new_view` AS
Select employee_id,employee_password,First_Name,Middle_Initial,
Last_Name,Date_Of_Birth,Beneficiaries,Home_Number,Address,Mobile_Number,Email_Address
From USER
LEFT JOIN personal_info on idUser = idPersonal_Info,
FULL JOIN contact_info on idUser = idContact_Info
The error is
ERROR 1146: Table 'payroll.full' doesn't exist
SQL Statement:
CREATE OR REPLACE VIEW `payroll`.`new_view` AS
Select employee_id,employee_password,First_Name,Middle_Initial,
Last_Name,Date_Of_Birth,Beneficiaries,Home_Number,Address,Mobile_Number,Email_Address
From USER
LEFT JOIN personal_info on idUser = idPersonal_Info,
FULL JOIN contact_info on idUser = idContact_Info
quote it with backtics: payroll.new_view
CREATE VIEW `payroll.new_view`
Error on:
LEFT JOIN personal_info on idUser = idPersonal_Info
you need to specify which column on which table equals which one on the other table, like
SELECT a,b,c from table1
LEFT JOIN table2
on table1.a= table2.columnY
in your case:
on USER.idUser = Personal_Info.idPersonalInfo
and the same for the 3rd Join
Another thing is the Comma at the end of the line:
LEFT JOIN personal_info on idUser = idPersonal_Info ,
it doesnt belong there.