This is my query:
Dim bugs = (From b In bugCon.bugs Where sctUserIds.Contains(b.Developer.Value) Order By b.bg_id Select Bug = b, Project = b.project).ToList
Currently this does an inner join between "bugs" and "projects". How do I turn it into a left join?
I haven't tested this, but the query below should get you headed in the right direction. The key is the join ... into syntax and the use of DefaultIfEmpty()
from b in context.Bugs
join p in context.Projects
on b.projectID equals p.projectID into BugProjects
where sctUserIds.Contains(b.Developer.Value)
from bugProjects in BugProjects.DefaultIfEmpty()
select new {
Name = p.Name,
...
BugProjects = bugProjects
}
Related
I have successfully written this join query below to get all the data I need from each individual table in the database within one query. Unfortunately, it gives me ALL the data from these individual tables. I only need data that corresponds to e.Design_ID, if e.Design_ID = 1 I have tried a few "where e.Design_ID = 1" within this code block, but have not been successful in restricting the query. I'm stuck. I'm thinking I may need to restructure my entire query. My logic may be off...
Any help is much appreciated.
SELECT
a.SKU,
a.Apparel_ID,
a.Apparel_Color_Abbr,
a.Design_ID,
a.City_ID,
a.Category_ID,
a.SEO_Keyword,
a.OC_Product_ID,
b.Apparel_Color_Palette,
b.Apparel_Name,
b.Apparel_Name_Abbr,
b.Apparel_Type,
b.Google_Feed_Cat,
b.Gender,
b.Age_Group,
b.Retail_Price,
b.Apparel_Description,
c.Apparel_Color,
d.OC_Cat_City_ID,
d.OC_Cat_State_ID,
d.City,
d.State,
e.Design_Name,
e.Design_Description
FROM Complete_City_Products AS a
INNER JOIN Apparel AS b ON a.Apparel_ID = b.Apparel_ID
INNER JOIN Apparel_Colors AS c ON b.Apparel_Color_Palette = c.Apparel_Color_Palette AND a.Apparel_Color_Abbr = c.Apparel_Color_Abbr
INNER JOIN Cities AS d ON a.City_ID = d.City_ID
INNER JOIN Designs AS e ON a.Design_ID = e.Design_ID
";
SELECT
a.SKU,
a.Apparel_ID,
a.Apparel_Color_Abbr,
a.Design_ID,
a.City_ID,
a.Category_ID,
a.SEO_Keyword,
a.OC_Product_ID,
b.Apparel_Color_Palette,
b.Apparel_Name,
b.Apparel_Name_Abbr,
b.Apparel_Type,
b.Google_Feed_Cat,
b.Gender,
b.Age_Group,
b.Retail_Price,
b.Apparel_Description,
c.Apparel_Color,
d.OC_Cat_City_ID,
d.OC_Cat_State_ID,
d.City,
d.State,
e.Design_Name,
e.Design_Description
FROM Complete_City_Products AS a
INNER JOIN Apparel AS b ON a.Apparel_ID = b.Apparel_ID
INNER JOIN Apparel_Colors AS c ON b.Apparel_Color_Palette = c.Apparel_Color_Palette AND a.Apparel_Color_Abbr = c.Apparel_Color_Abbr
INNER JOIN Cities AS d ON a.City_ID = d.City_ID
INNER JOIN Designs AS e ON a.Design_ID = e.Design_ID
WHERE e.Design_ID = 1
";
It should work if you put it at the end. Also check correct spelling.
I'm having trouble with a simple MySQL Query.
Here is the query:
SELECT distinct e.E_CODE, s.S_CODE, p.P_ID, p.P_NAME, p.P_FIRSTNAME, p.P_STATUS, e.E_BOSS, tp.TP_TITLE
from event_participation ep, worker p, type_participation tp, event e, section s
where ep.P_ID = p.P_ID
and s.S_ID = e.S_ID
and ep.TP_ID = tp.TP_ID
and e.E_CODE = ep.E_CODE
The problem is that ep.TP_ID sometimes has a value set to zero while tp.TP_ID has nothing with a zero ID. It's auto-increment and starts at 1 and so on.
The result is obviously that this query does not return records when the ep.TP_ID = 0 and there is no match in tp.TP_ID.
So I'm trying to figure out a way to get those results in there anyway. I was thinking of using a LEFT JOIN statement but couldn't figure out a proper way to insert it into the query.
Any advice on this matter would be greatly appreciated.
First of all, I advice you to use some general type for event_participation records without type; But, unless to take that decision, supposing you want to get all matching records between all tables but also get results with no type, you can use the following query:
SELECT DISTINCT e.E_CODE, s.S_CODE, p.P_ID, p.P_NAME, p.P_FIRSTNAME, p.P_STATUS, e.E_BOSS, tp.TP_TITLE
FROM event_participation ep
JOIN worker p ON (ep.P_ID = p.P_ID)
JOIN event e ON (e.E_CODE = ep.E_CODE)
JOIN section s ON (s.S_ID = e.S_ID)
LEFT JOIN type_participation tp ON (ep.TP_ID = tp.TP_ID)
SELECT DISTINCT e.E_CODE
, s.S_CODE
, p.P_ID
, p.P_NAME
, p.P_FIRSTNAME
, p.P_STATUS
, e.E_BOSS
, tp.TP_TITLE
FROM event_participation ep
JOIN worker p
ON p.P_ID = ep.P_ID
JOIN event e
ON e.E_CODE = ep.E_CODE
JOIN section s
ON s.S_ID = e.S_ID
LEFT
JOIN type_participation tp
ON tp.TP_ID = ep.TP_ID;
I am trying to run the following query:
SELECT `aalv_test`.`aircraft`.*, `aalv_test`.`airports`.*, `aalv_test`.`bids`.*
FROM `bids`
LEFT JOIN `aalv_test`.`pilots` ON `bids`.`pid` = `pilots`.`id`
LEFT JOIN `aalv_test`.`schedules` ON `bids`.`fid` = `schedules`.`id`
LEFT JOIN `aalv_test`.`aircraft` ON `schedules`.`aircraft` = `aircraft`.`id`
LEFT JOIN `aalv_test`.`airports` AS `arr` ON `schedules`.`arricao` = `arr`.`icao`
LEFT JOIN `aalv_test`.`airports` AS `dep` ON `schedules`.`depicao` = `dep`.`icao`
WHERE `pilots`.`id` = 419
However,
MYSQL returns error #1051 - Table airports does not exist.
I don't know what the issue is and Google hasn't helped. Any ideas? Also, if I only use one alias, I only get one airport but I need both. And the data is only in the table airports which according to this query, does not exist. Also, if I try throwing an AS section in the SELECT clause, I get error 1064: syntax error near AS.
EDIT: Database name is aalv_test, the .* at the end specifies to use all fields in the table, and the middle part is the table name, yes I am chaining fields.
Try this:
SELECT a.*, arr.*, dep.*, b.*
FROM bids AS b
LEFT JOIN aalv_test.pilots AS p ON b.pid = p.id
LEFT JOIN aalv_test.schedules AS s ON b.fid = s.id
LEFT JOIN aalv_test.aircraft AS a ON s.aircraft = a.id
LEFT JOIN aalv_test.airports AS arr ON s.arricao = arr.icao
LEFT JOIN aalv_test.airports AS dep ON s.depicao = dep.icao
WHERE p.id = 419;
I'm trying to convert this SQL to HQL, but I found no way to do that left join.
SELECT
mdcs_causa.id_causa,
usuarios.ds_usuario,
usuarios.setor,
empresas.ds_empresa,
itens_controle.id_item_controle,
itens_controle.ds_item,
itens_controle.ds_indicador,
itens_controle.ds_cliente,
itens_controle.desdobramento,
itens_controle.auxiliar,
itens_controle.bmk_nome,
itens_controle.bmk_vlr,
m.status,
m.medido,
m.medicao,
m.fca,
m.am_cronico,
m.ac_cronico,
m.ap_cronico,
m.id_medicoes as idmedicao,
itens_controle.prioridade
FROM
mdcs
INNER JOIN mdcs_causa
ON mdcs.id_mdc = mdcs_causa.id_mdc
INNER JOIN itens_controle
ON mdcs_causa.Id_Item_Controle = itens_controle.id_item_controle
INNER JOIN usuarios
ON usuarios.id_usuario = itens_controle.id_usuario
INNER JOIN empresas
ON empresas.id_empresa = usuarios.id_cliente_tabela
LEFT JOIN medicoes m
ON (
itens_controle.id_item_controle = m.id_item_controle
and
m.nm_ano = 2013
and
m.nm_periodo = 2
and
mdcs_causa.id_mdc = mdcs.id_mdc
)
WHERE
mdcs.id_mdc = 5077
I thought I could put the nm_ano and nm_periodo conditions in the where clause, with an OR to m.id_item_controle is null but this OR condition didn't seem to work, even in SQL.
Another approach was to left join a sub query. That worked in SQL, but I think HQL doesn't support that.
It can be done with with keyword:
...
left join itens_controle.medicoes m with (m.nm_ano = 2013 and m.nm_periodo = 2 and mdcs_causa.id_mdc = mdcs.id_mdc)
where mdcs.id_mdc = 5077
Instead of explicit values you should supply them as arguments.
Read this in this pdf left join is used
as below:
Query getEBills =session.createQuery( session.createQuery( from " EBill ebill EBill
ebill
left join ebill.accountTransaction where
ebill.balance > 500";
Li li f l bi i li () ist listOfRowValues = getDebitTransactions.list();
for (Object[] singleRowValues : listOfRowValues) {
// pull off the EBill // pull off the EBill
EBill ebill = (EBill)singleRowValues[0];
I dont have any experience in HQL, i have pasted what i have found for you. hope this will help you in any way.
I have this linq query :
(from rapportBase in New_RapportReferencementBases
join rapportExtensionAll in New_RapportReferencementExtensionBases on rapportBase.New_RapportReferencementId equals rapportExtensionAll.New_RapportReferencementId into jointureRapportExtension
from rapportExtension in jointureRapportExtension.DefaultIfEmpty()
join packExtensionAll in New_PackExtensionBases on rapportExtension.New_PackId equals packExtensionAll.New_PackId into jointurePackExtension
from packExtension in jointurePackExtension.DefaultIfEmpty()
join packBaseAll in New_PackBases on packExtension.New_PackId equals packBaseAll.New_PackId into jointurePackBase
from packBase in jointurePackBase.DefaultIfEmpty()
join domaineBaseAll in New_DomaineBases on packExtension.New_DomaineId equals domaineBaseAll.New_DomaineId into jointureDomaineBase
from domaineBase in jointureDomaineBase.DefaultIfEmpty()
join domaineExtensionAll in New_DomaineExtensionBases on domaineBase.New_DomaineId equals domaineExtensionAll.New_DomaineId into jointureDomaineExtension
from domaineExtension in jointureDomaineExtension.DefaultIfEmpty()
join compteBaseAll in AccountBases on domaineExtension.New_AccountId equals compteBaseAll.AccountId into jointureCompteBase
from compteBase in jointureCompteBase.DefaultIfEmpty()
join compteExtensionAll in AccountExtensionBases on compteBase.AccountId equals compteExtensionAll.AccountId into jointureCompteExtension
from compteExtension in jointureCompteExtension.DefaultIfEmpty()
select rapportBase)
which generate :
SELECT [t0].[New_RapportReferencementId], [t0].[CreatedOn], [t0].[CreatedBy], [t0].[ModifiedOn], [t0].[ModifiedBy], [t0].[OwningUser], [t0].[OwningBusinessUnit], [t0].[statecode] AS [Statecode], [t0].[statuscode] AS [Statuscode], [t0].[DeletionStateCode], [t0].[VersionNumber], [t0].[ImportSequenceNumber], [t0].[OverriddenCreatedOn], [t0].[TimeZoneRuleVersionNumber], [t0].[UTCConversionTimeZoneCode]
FROM [New_RapportReferencementBase] AS [t0]
LEFT OUTER JOIN [New_RapportReferencementExtensionBase] AS [t1] ON [t0].[New_RapportReferencementId] = [t1].[New_RapportReferencementId]
LEFT OUTER JOIN [New_PackExtensionBase] AS [t2] ON [t1].[New_PackId] = ([t2].[New_PackId])
LEFT OUTER JOIN [New_PackBase] AS [t3] ON [t2].[New_PackId] = [t3].[New_PackId]
LEFT OUTER JOIN [New_DomaineBase] AS [t4] ON [t2].[New_DomaineId] = ([t4].[New_DomaineId])
LEFT OUTER JOIN [New_DomaineExtensionBase] AS [t5] ON [t4].[New_DomaineId] = [t5].[New_DomaineId]
LEFT OUTER JOIN [AccountBase] AS [t6] ON [t5].[New_AccountId] = ([t6].[AccountId])
LEFT OUTER JOIN [AccountExtensionBase] AS [t7] ON [t6].[AccountId] = [t7].[AccountId]
But I want to generate :
SELECT [t0].[New_RapportReferencementId], [t0].[CreatedOn], [t0].[CreatedBy], [t0].[ModifiedOn], [t0].[ModifiedBy], [t0].[OwningUser], [t0].[OwningBusinessUnit], [t0].[statecode] AS [Statecode], [t0].[statuscode] AS [Statuscode], [t0].[DeletionStateCode], [t0].[VersionNumber], [t0].[ImportSequenceNumber], [t0].[OverriddenCreatedOn], [t0].[TimeZoneRuleVersionNumber], [t0].[UTCConversionTimeZoneCode]
FROM [New_RapportReferencementBase] AS [t0]
FULL OUTER JOIN [New_RapportReferencementExtensionBase] AS [t1] ON [t0].[New_RapportReferencementId] = [t1].[New_RapportReferencementId]
FULL OUTER JOIN [New_PackExtensionBase] AS [t2] ON [t1].[New_PackId] = ([t2].[New_PackId])
FULL OUTER JOIN [New_PackBase] AS [t3] ON [t2].[New_PackId] = [t3].[New_PackId]
FULL OUTER JOIN [New_DomaineBase] AS [t4] ON [t2].[New_DomaineId] = ([t4].[New_DomaineId])
FULL OUTER JOIN [New_DomaineExtensionBase] AS [t5] ON [t4].[New_DomaineId] = [t5].[New_DomaineId]
FULL OUTER JOIN [AccountBase] AS [t6] ON [t5].[New_AccountId] = ([t6].[AccountId])
FULL OUTER JOIN [AccountExtensionBase] AS [t7] ON [t6].[AccountId] = [t7].[AccountId]
In other word, i want to generate full outer join for this query and not just left.
someone know how to do this in a simple way ?
thanks
There isn't a Full Outer Join in Linq. You have to do two left joins and concat them together. Here's some pseudocode that looks like linq:
var foj =
(from l in left
join r in right on l.Id equals r.Id into g
from r in g.DefaultIfEmpty()
select new { l, r })
.Concat
(from r in right
join l in left on r.Id equals l.Id into g
from l in g.DefaultIfEmpty()
where l == null
select new { l, r });
Probably be better to push this logic into a stored procedure if you're planning on using Linq to Sql.
See this post from the VB Team:
http://blogs.msdn.com/vbteam/archive/2008/02/12/converting-sql-to-linq-part-9-full-outer-join-bill-horst.aspx
Looks a little verbose.
Otherwise, there may be a way to achieve what you want using Foreign Keys and the Union operator.
Sadly I saw that before but i wanted to be sure.
The thing we did is to do it in a stored procedure and access it via linqtosql method.
then we map table result, like linqtosql do, using grouping to obtain collection of enties link to another entity.
for example, if we have an account with some contacts link to it. We request all contact with for each of them the account with a full join and them group on account id to obtain list of contact of each.
It work very well but it would be better if linqtosql was able to generate full join....