linq to sql left outer join not giving expected results - linq-to-sql

I have been trying to get the below SQL query to give me same result while using in Linq but i am not getting desired result. I know there are some other posts regarding left outer join in linq but they don't give me expected result so i thought to put query itself. Kindly provide me with linq query for below SQL query.
Select vbk.*,
adm.admFullName
from Voucherbooks vbk
left outer join adminlogins adm
on vbk.vbkauthid = adm.admid
Thanks in advance.

var results = Voucherbooks
.GroupJoin
(
adminlogins,
x=>x.vbkauthid,
x=>x.admid
(vbk,adm)=>new
{
vbk,
admFullName = adm.FirstOrDefault() == null
? String.Empty
: adm.FirstOrDefault().admFullName
}
)

Related

SQL query returning all row's values however using conditions

I don't know why but the sql query is returning all rows however I have defined the conditions as well.
When I click this link:
'.$student_class.$class_section.'
It runs the following query.
The mySQL query used is:
$query = $db->query("
SELECT a.*, att.*, aatt.*
FROM ".TABLE_PREFIX."school_att_attendance aatt
LEFT JOIN ".TABLE_PREFIX."school_att att ON (att.att_date=aatt.att_date)
LEFT JOIN ".TABLE_PREFIX."student_id_card a ON (a.sid=aatt.sid)
WHERE att.att_class = '{$mybb->input['c']}' AND att.att_section = '{$mybb->input['s']}' AND att.att_date = '{$mybb->input['d']}' AND att.att_month = '{$mybb->input['m']}' AND att.att_year = '{$mybb->input['y']}'
ORDER BY aatt.sid ASC
");
and then returns all rows irrespective of the conditions used. I am sure something I am doing wrong here but I am stucked here. Please help.

How to convert SQL query to HQL query with two tables and LEFT JOIN

I have a problem when I'm trying to convert a SQL query to HQL.
SQL Query
SELECT RahHistoSaipAcogida.numicu, RahHistoSaipAcogida.fecharealiza, TaSaip.tipo_descri AS [Tipo Acogida], RahHistoSaipAcogida.paccomunica, RahHistoSaipAcogida.presentaciĆ³n, RahHistoSaipAcogida.infohlp, RahHistoSaipAcogida.infovolant, RahHistoSaipAcogida.infoddp, RahHistoSaipAcogida.observaciones, RahHistoSaipAcogida.nom_prof, RahHistoSaipAcogida.autoriza_informacion
FROM RahHistoSaipAcogida
LEFT JOIN TaSaip ON RahHistoSaipAcogida = TaSaip.tipo_codi
WHERE RahHistoSaipAcogida.fecharealiza BETWEEN :desde AND :hasta
HQL Query
session1.createQuery("FROM RahHistoSaipAcogida acog, TaSaip ts LEFT JOIN ts ON acog.tipo = ts.tipo_codi WHERE acog.fecharealiza BETWEEN :desde AND :hasta")
.setParameter("desde", fechadesde)
.setParameter("hasta", fechahasta);
stacktrace
ERROR: Path expected for join! Path expected for join!
If anyone can help me, I would be very grateful. Thanks
Your query must look like:
FROM RahHistoSaipAcogida acog LEFT JOIN acog.ts WHERE acog.fecharealiza BETWEEN :desde AND :hasta
You working on objects and there properties not on tables

Performing multiple joins while using IS NOT NULL in MySQL

I have several tables that I am selecting data from. I am trying to get a list of information out of these tables only where the fields
"interested_parties.emp_id" and "solicitation_entrys.sol_ent_id" are satisfied.
This is my query that I have come up with so far:
SELECT * FROM users
JOIN interested_parties ON (users.emp_id = interested_parties.emp_id)
JOIN department_colors ON (department_colors.dept_id = users.emp_dept)
JOIN solicitation_entrys ON (solicitation_entrys.sol_ent_id = interested_parties.sol_ent_id)
WHERE IS NOT NULL (interested_parties.emp_id, solicitation_entrys.sol_ent_id)
The goal is to select everything from these tables, where these fields are not null.
I'm getting #1064 errors from MySQL and I can't seem to solve the issue. Do I have to perform the if at the beginning of the select? If so, how would you do that with joins?
Thank you for your help.
Use this instead
SELECT * FROM users
JOIN interested_parties ON (users.emp_id = interested_parties.emp_id)
JOIN department_colors ON (department_colors.dept_id = users.emp_dept)
JOIN solicitation_entrys ON (solicitation_entrys.sol_ent_id = interested_parties.sol_ent_id)
WHERE interested_parties.emp_id IS NOT NULL
AND solicitation_entrys.sol_ent_id IS NOT NULL
You have to compare each field for not being null.
As MySQL should telling you, the error is in your SQL syntax.
The WHERE clause should be:
WHERE (interested_parties.emp_id IS NOT NULL)
AND (solicitation_entrys.sol_ent_id IS NOT NULL)

LINQ 2 SQL :Adding one more table to left join in LINQ 2 SQL statement

I am a newbir to LINQ 2 SQL and needs help to create left join query.
I have the below LINQ 2 SQL query to get data from 2 tables.Now i want to LEFT join one more table to this .A column called "SerialId" is associated with SerialId column in "IV00200s " table
Dim items=(From i In oRecelDB.IV00200s From c In oRecelDB.MDS_CONTAINERs.Where
(Function(c) c.CONTAINERBIN = i.BIN).DefaultIfEmpty() Select New With
{i.ITEMNMBR, i.SERLNMBR, i.BIN, c.LOCNCODE}).Take(15)
Can anyone help me to frame the statement
Not quite sure I understand what table you wish to left outer join with, but i'll have a stab at it - in C# though...
var items = (from i in oRecelDB.IV00200s
join c in oRecelDB.MDS_CONTAINERs on i.CONTAINERBIN equals i.BIN
join ot in oRecelDB.OtherTable on i.SerialId equals nt.SerialId into tmpOtherTable
from tmpOT in tmpOtherTable.DefaultIfEmpty()
Select New
{
i.ITEMNMBR,
i.SERLNMBR,
i.BIN,
c.LOCNCODE,
AColumn = (tmpOT == null ? null : tmpOT.AColumn)
}).Take(15);

MYSQL get other table data in a join

I am currently running this SQL
SELECT jm_recipe.name, jm_recipe.slug
FROM jm_recipe
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
This returns the desired results except that I also need to return the name of the category that the recipe I am looking for is in, to do this I tried to add the field in to my SELECT statement and also add the table into the FROM clause,
SELECT jm_recipe.name, jm_recipe.slug, jm_category_name
FROM jm_recipe, jm_category
LEFT JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
WHERE jm_category_recipe.category_id = $cat"
However this just returns no results, what am i doing wrong?
You need to join both tables:
SELECT jm_recipe.name, jm_recipe.slug, jm.category_name
FROM jm_recipe
INNER JOIN jm_category_recipe ON jm_category_recipe.recipe_id = jm_recipe.id
INNER JOIN jm_category ON jm_recipe.recipe_id = jm_category.recipe_id
WHERE jm_category_recipe.category_id = $cat
I've changed the joins to inner joins as well. You might want to make them both LEFT joins if you have NULLs and want them in the result.
Also, you're vulnerable to SQL Injection by simply copying over $cat.
Here's some PHP specific info for you (I'm assuming you're using PHP.)