I'm new to Linq to SQL and I'm trying to transform this SQL into Linq. Could you please help me out.
SELECT *
FROM [dbo].[tblTest]
INNER JOIN [dbo].[tblStationTest] ON [tblTest].[id] = [tblStationTest].[Test_id]
INNER JOIN [dbo].[tblTestType] ON [tblTest].[TestType_id] = [tblTestType].[id]
LEFT OUTER JOIN [dbo].[tblTestOrder] ON [tblTest].[id] = [tblTestOrder].[Test_id]
AND ([tblTestOrder].[TestOrderList_id] = 1)
WHERE ([Station_id] = 1)
What is causing me problem is this condition AND ([TestOrderList_id] = 1)
This condition can't be in Where clase because it will cancel the effect of Left Join
Thanks kurin
you can try something like
from test in db.tblTest
join stationTest in db.tblStationTest on test.id equals stationTest.Test_id
join testType in db.tblTestType on test.TestType_id equals testType.id
join testOrder in db.tblTestOrder on new{Key1 = test.id, Key2= 1} equals new{Key1 = testOrder.Test_id, Key2 = testOrder.TestOrderList_id} into tempOrders
from t in tempOrders.DefaultIfEmpty()
select new{test.Test_id, testType.something, t.somethingelse}
Related
I don't know why my case code is having an error but when I execute it not using a parameter by replacing the #PersonId by 2 it runs well. I try to exchange2x the JOIN but I am still having an error. Anyone knows?.
case when ISNULL(dbo.EducationalBackground.SchoolId,'') = '' then
(Select a.SchoolName
from dbo.EducationalBackground as A INNER JOIN
dbo.PersonEducationalBackground as B on a.EducationalBackgroundId = b.EducationalBackgroundId INNER JOIN
dbo.EducationLevel as C on a.EducationalLevelId = c.EducationLevelId
where b.PersonId = #PersonId and a.EducationalLevelId in (2,3))
else (select dbo.school.SchooldName from dbo.School INNER JOIN dbo.EducationalBackground
ON dbo.School.SchoolId = dbo.EducationalBackground.SchoolId INNER JOIN
dbo.PersonEducationalBackground ON dbo.EducationalBackground.EducationalBackgroundId = dbo.PersonEducationalBackground.EducationalBackgroundId
where dbo.PersonEducationalBackground.PersonId = #PersonId)
Cheers. Thanks.
I am using following query to retrieve the needed objects:
SELECT *
FROM tb_po_items
LEFT JOIN tb_materials ON tb_po_items.po_material = tb_materials.id_material
LEFT JOIN tb_services ON tb_po_items.po_service = tb_services.id_service
WHERE po_id =47
Now I need to add following:
Condition 1 -> if tb_po_items.mos = 1 then LEFT JOIN tb_units ON tb_materials.material_unit = tb_units.id_unit
else
Condition 2 -> if tb_po_items.mos = 2 then LEFT JOIN tb_units ON tb_services.service_unit = tb_units.id_unit
How can I implement the use of both conditions in the MySQL query?
Thanks in advance.
Try this:
SELECT *
FROM tb_po_items
LEFT JOIN tb_materials ON tb_po_items.po_material = tb_materials.id_material
LEFT JOIN tb_services ON tb_po_items.po_service = tb_services.id_service
LEFT JOIN tb_units ON (
(tb_materials.material_unit = tb_units.id_unit AND tb_po_items.mos = 1)
OR
(tb_services.service_unit = tb_units.id_unit AND tb_po_items.mos = 2) )
WHERE po_id =47;
hi this is experimental so check before you go live.
SELECT * FROM tb_po_items
IF(tb_po_items.mos = 1, LEFT JOIN tb_materials ON tb_po_items.po_material = tb_materials.id_material, LEFT JOIN tb_services ON tb_po_items.po_service = tb_services.id_service)
WHERE po_id =47
I have a question in my query. why I can't filter using a.train_num?. The result is always 0. But when I filter using c.stridnumber then I am getting result.
please check my below query.
SELECT a.*,b.*,c.*
FROM pos_train_db a
INNER JOIN emp_db b
ON a.pos_name = b.emp_position
INNER JOIN tms_ml c
ON b.ID = c.stridnumber
WHERE a.train_num=10
Try this:
$this->sql =
"SELECT a.*,b.*,c.* FROM pos_train_db a
LEFT JOIN emp_db b ON a.pos_name = b.emp_position
LEFT JOIN tms_ml c ON b.ID = c.stridnumber
WHERE a.train_num=10 ";
I want to do a left join but mysql just does an inner join?
whats wrong with my query?
select av.*, ap.*
from tbl_available av
left join tbl_appointment ap
on av.avHours = ap.appointmenttime
where av.avCalendarId = 2
and (ap.calendarid = 2 or ap.calendarid= null)
and (ap.appointmentdate = "2012-10-01" or ap.appointmentdate = null)
and av.avDays = DayOfweek("2012-10-01")
order by avHours
mysql only gives those avHours who have a corresponding appointment
Thanks in advance!
Because of these conditions:
and ap.calendarid = 2
and ap.appointmentdate = "2012-10-01"
you only select rows from tbl_appointment which are not null.
If that's what you want - move them to the left join's ON part
i need an sql stament that will give me something like this where if a field is null it doesn't do the join
SELECT AdminID,tblapartments.NameNo, tblgarages.GarageID, tblclients.Name FROM tbladmin,tblclients,tblgarages,tblapartments WHERE tblclients.ClientID =tbladmin.ClientID AND
IF (tbladmin.ApartmentID != null)
{
tblapartments.ApartmentID = tbladmin.ApartmentID
}
AND If(tbladmin.GarageID != Null)
{
tblgarges.GarageID = tbladmin.GarageID
}
Unless I'm missing something, this should just be an outer join.
SELECT
AdminID,
tblapartments.NameNo,
tblgarages.GarageID,
tblclients.Name
FROM
tbladmin INNER JOIN tblclients ON tbladmin.ClientID = tblclients.ClientID
LEFT OUTER JOIN tblgarages ON tbladmin.GarageID = tblgarages.GarageID
LEFT OUTER JOIN tblapartments ON tbladmin.ApartmentId = tblapartments.ApartmentID
You can use LEFT JOINs, when the joined column does not exist in the other table the result is a lot of NULL fields:
SELECT AdminID,tblapartments.NameNo, tblgarages.GarageID, tblclients.Name
FROM tbladmin
INNER JOIN tblclients
ON tbladmin.ClientID = tblclients.CliendID
LEFT JOIN tblgarages
ON tbladmin.GarageID = tblgarages.GarageID
LEFT JOIN tblapartments
ON tbladmin.ApartmentID = tblapartments.ApartmentID
I do not believe that this type of if logic is SQL standard. You could possibly implement it in a procedural SQL langauge like PL/SQL, plpgsql ... however to accomplish what you after i think a left join what you should look at.
SELECT AdminID,tblapartments.NameNo, tblgarages.GarageID, tblclients.Name
FROM tbladmin a
join tblclients b on b.ClientID = a.ClientID
left join tblapartments c on c.ApartmentID = a.ApartmentID
left join tblgarges d on d.GarageID = a.GarageID