After quick overview I didn't find how to deal with linq to sql if I have several nested inner joins.
That's what I'd like to have in linq
SELECT Booking.BookingId, Booking.EventId, Booking.StartDate, Event.Name, Person.FirstName
FROM Booking
INNER JOIN Event
INNER JOIN Asset
ON Asset.AssetId = Event.AssetId
INNER JOIN Person
ON Person.PersonId = Event.ContactPersonId
ON Event.EventId = Booking.EventId AND Event.State = 4
Does anyone know how to translate it to LINQ?
Thanks.
var q1= from a in booking,b in event,c in asset, d in person where a.eventid=b.eventid and b.state=4 and c.assetid = b.assetid and b.contactpersonid=d.personid select a,b,c,d
you can replace a,b,c,d at then with the column names you want
another way is to use the join keyword:
var w1= from a in booking join b in event on a.eventid equals b.eventid join c in asset on ...
var query = from b in context.Bookings
from e in context.Events
join a in context.Assets on e.AssetId equals a.AssetId
join p in context.People on e.ContactPersonId equals p.PersonId
where e.State == (byte)States.Approved && e.EventId == b.EventId
select new { EventName = e.Name, BookingDate = b.StartDate };
Related
$sql = "SELECT cc.name AS c_name, ev.name AS event_name, ev.eventModeId AS event_mode, ev.carClassHash, cc.carClassHash
FROM EVENT_DATA e
INNER JOIN PERSONA p ON e.personaId = p.ID
INNER JOIN CUSTOMCAR cc ON cc.ownedCarId = e.carId
INNER JOIN CAR_CLASSES ccs ON ccs.store_name = cc.name
INNER JOIN USER u ON u.ID = p.USERID
LEFT JOIN BAN b ON b.user_id = u.ID
INNER JOIN EVENT ev ON ev.ID = e.EVENTID
WHERE (p.name = ?
AND ev.carClassHash = cc.carClassHash)";
This query works for me except I'd also like to display any carClassHash with the value '607077938'. Is there a way I could somehow keep the above query to check if the event hash matches the car class hash but also still display values where the carClassHash (cc.carClassHash) is equal to '607077938'?
Thanks! :)
Why not use OR in the WHERE clause?
[...]
WHERE ((p.name = ? AND ev.carClassHash = cc.carClassHash) OR (cc.carClassHash = 607077938))
This should work if that value is an integer. If it's a string, use quotes around it.
looking for a bit of help here if possible?
I have the following query:-
On or database we have a table called Linkfile, in this table are "Types" all beginning with "YG". I need to return those rows that do not have the type of "YG8" but just cannot seem to do it. I know ill need to use a sub query but am stuck!
This is my code and the fields I need to return. I just need to only show those that do not have the lk.type of "YG8"
select distinct l.description, p.displayname AS Temp, p.compliance_status As 'Compliant', lk.displayname, lk.type
from event e
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on p.person_ref = pt.person_ref
inner join linkfile lk on lk.parent_object_ref = pt.person_ref
where o.displayname LIKE '%G4S%' and p.compliance_category = '$016'
and lk.type like 'YG%' and l.code_type = '2'
and a.displayname LIKE '%MOJ%'
and pt.status = 'A'
order by l.description, p.displayname, lk.type
Use below query :
select distinct l.description, p.displayname AS Temp, p.compliance_status As 'Compliant', lk.displayname, lk.type,lk.parent_object_ref
from event e
inner join organisation o on e.organisation_ref = o.organisation_ref
inner join opportunity opp on e.opportunity_ref = opp.opportunity_ref
inner join event_role ev on ev.event_ref = e.event_ref
inner join address a on a.address_ref = opp.address_ref
inner join person p on ev.person_ref = p.person_ref
inner join lookup l on p.responsible_team = l.code
inner join person_type pt on p.person_ref = pt.person_ref
left join (select displayname, type,parent_object_ref from linkfile where lk.type like 'YG8%' )lk on lk.parent_object_ref = pt.person_ref
where o.displayname LIKE '%G4S%' and p.compliance_category = '$016' and lk.parent_object_ref is null
and l.code_type = '2'
and a.displayname LIKE '%MOJ%'
and pt.status = 'A'
order by l.description, p.displayname, lk.type;
I've used left join on linkfile with type like 'YG8%' and fetching the only records which are not matched
I think you can just replace the
lk.type like 'YG%'
with the following:
(lk.type >= 'YG' and lk.type <'YG8') or (lk.type > 'YG8' and lk.type <='YGZ')
this should accomplish what you are trying to do and also avoid using "like" which is less efficient (assuming you have an index on lk.type, at least).
You may refine this a bit by knowing which are the possible values of lk.type of course. I.e. what are the extremes for the YG "subtype"? YG00-YG99? YG-YGZ?
(Be especially careful if you may have YG81 or YG87 for example, because then my clause will not work properly... on the other hand if your YG subtype can have values like YG34 it would have been better to use YG08 instead of YG8)
First, the table names and layouts:
Here are my desired results:
Here is the 'got ya' (trick) I guess..
I will only be passing a (dynamic) imis_id/user_id (col names currently not matching on one table)...
So a lookup (select) will need to be done on the relations table for that passed in (dynamic) id.. and where current_org = 1.
This will get/give me the target org_id in which I need to grab all user info (that are associated with the org_id).. and all org details.
Here is one weak/failed attempt: (it uses a hardcoded org_id) which is not valid.. I need to ONLY pass in the user_id/imis_id.. and where current_org = 1 to get the target org_id.
SELECT genealogy_orgs.org_id, genealogy_orgs.org_name,
genealogy_relations.user_id, genealogy_relations.relation_type, genealogy_relations.start_year, genealogy_relations.end_year,
genealogy_users.imis_id, genealogy_users.full_name
FROM genealogy_orgs
INNER JOIN genealogy_relations ON genealogy_orgs.org_id = genealogy_relations.org_id
INNER JOIN genealogy_users ON genealogy_relations.user_id = genealogy_users.imis_id
WHERE genealogy_orgs.org_id = '84864';
Here is another failed attempt (which only returns 1 row).. but uses the correct criteria:
SELECT
genealogy_relations.org_id,
genealogy_relations.user_id, genealogy_relations.relation_type, genealogy_relations.start_year, genealogy_relations.end_year,
genealogy_users.imis_id, genealogy_users.full_name,
genealogy_orgs.org_name
FROM genealogy_relations
INNER JOIN genealogy_orgs ON genealogy_relations.org_id = genealogy_orgs.org_id
INNER JOIN genealogy_users ON genealogy_relations.user_id = genealogy_users.imis_id
WHERE genealogy_relations.user_id = '00003' AND genealogy_relations.current_org = '1';
At this point, I'm not even sure what I need to search for? Is this where a 'sub-query/sub-select' comes into play?
My MySQL-fu is limited to very direct/plain-jane query types. This is getting to be more advanced than I am used to.
You need to join the genealogy_relations table with itself on org_id:
SELECT o.*, u.*
FROM genealogy_relations r1
JOIN genealogy_relations r2 ON r2.org_id = r1.org_id
JOIN genealogy_orgs o ON o.org_id = r2.org_id
JOIN genealogy_users u ON u.imis_id = r2.user_id
WHERE r1.user_id = '00003'
AND r1.current_org = '1'
Remove the genealogy_relations.user_id = '00003' from the query and it should work
SELECT
genealogy_relations.org_id,
genealogy_relations.user_id, genealogy_relations.relation_type, genealogy_relations.start_year, genealogy_relations.end_year,
genealogy_users.imis_id, genealogy_users.full_name,
genealogy_orgs.org_name
FROM genealogy_relations
INNER JOIN genealogy_orgs ON genealogy_relations.org_id = genealogy_orgs.org_id
INNER JOIN genealogy_users ON genealogy_relations.user_id = genealogy_users.imis_id
WHERE genealogy_relations.current_org = '1';
I want to use Outer Join with inner Join in a single query
Query:
select d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry
from bkng_mst a , bkng_pax b, bkng_cust c, unit_mst d
where a.bkng_id = b.bkng_id
and b.unit_cde = d.unit_cde
and a.unit_cde = d.unit_cde
and b.cust_id = c.cust_id
and a.bkng_stat = 'CNF'
and b.bkng_pax_cancel_flg = 'N'
and a.bkng_id = 'XXXX'
Use Outer Join from Table pax_dtl pd on a.bkng_id=pd.bkng_id along with above query
UPDATED :
I think that, taking into account the information provided in your comments, the following query should be helpful:
SELECT DISTINCT
d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry,
pd.bkng_id, pd.unit_name, pd.tour_code, pd.pax_name, pd.pnr_no, pd.fare_base, pd.is_block, pd.is_system
FROM
bkng_mst a
INNER JOIN bkng_pax b
ON a.bkng_id = b.bkng_id
INNER JOIN bkng_cust c
ON b.cust_id = c.cust_id
INNER JOIN unit_mst d
ON b.unit_cde = d.unit_cde
AND a.unit_cde = d.unit_cde
LEFT OUTER JOIN pax_dtl pd
ON a.bkng_id=pd.bkng_id
WHERE
a.bkng_stat = 'CNF'
AND b.bkng_pax_cancel_flg = 'N'
AND a.bkng_id = 'XXXX'
Because of 1 to many relation between bkng_mst and pax_dtl tables, the columns d.unit_name, a.tour_code, a.hub_code, b.name, c.pp_no, c.dte_of_expiry from above query will repeat only if for 1 particular bkng_id value there will be at least one different value among the columns pd.bkng_id, pd.unit_name, pd.tour_code, pd.pax_name, pd.pnr_no, pd.fare_base, pd.is_block.
I hope it might help you, but in case of any doubts please write.
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