Ok dumb question time. Linq to SQL. Join - linq-to-sql

I have two tables:
Clients, and a join table that has user id, a foreign key to clients, and some other stuff.
I want to do this SQL
select TblClient.* from TblClient
inner join tblUserClientProjJoin as b on TblClient.Client_ID = b.Client_FK
where b.User_fk = 2
So getting a list of 'clients' that a specific user has access to.
I want to apply this result to collection I can bind to controls.
Anyone?

var q = From c in db.TblClient join
ucp in db.tblUserClientProjJoin on
c.Client_ID equals ucp.Client_FK
select c;
I haven't tested this so you may need to modify it a little.
Hope this helps!

Related

Using Or in a sql statement which is operating in multiple tables

The Problem:
I have 2 tables i am selecting data from one called Table_Booked which contains one record with a Username and SessionID for each time a user books, another table called Table_Sessions which contains the Session details such as SessionId,Subject,TeacherID,Day,Am_Pm and WhatYear.
Im trying to select all the records from Table_Booked where the username is the same as the current users, and i also want to get the Day & Am_Pm data for each of the sessions the user has booked or the records for the session he is trying to book. The aim is to compare the records and if any occur in the same time slot not allow the user to book the session.
The Code:
Select Table_Session.Day,Table_Session.Am_Pm,Table_Session.SessionId
From Table_Session,Table_Booked
Where Table_Booked.Username = 'G9rpenman' and
((Table_Booked.SessionID = Table_Session.SessionId) Or (Table_Session.SessionId = 9))
When i run this code it fetches the information from Table_Session.SessionId = 9 twice and other tweaks have caused similarly random data selection. I cant find anything about this. Thank you for your help. If you feel i missed any crucial information out let me know and ill edit this.
Heres some screenshots of my tables with the data im using http://imgur.com/a/9fZtW
Simple rule: Never use commas in the FROM clause. Always use explicit JOIN syntax.
The reason for the duplication is the OR. I think this will do what you want:
Select s.Day, s.Am_Pm, s.SessionId
From Table_Booked b join
Table_Session s
on s.SessionID = b.SessionId or s.SessionId = 9
Where b.Username = 'G9rpenman' ;
If not, you may need union:
Select s.Day, s.Am_Pm, s.SessionId
From Table_Booked b join
Table_Session s
on s.SessionID = b.SessionId
Where b.Username = 'G9rpenman'
union
Select s.Day, s.Am_Pm, s.SessionId
from Table_Session s
where s.SessionId = 9;
Why don't you try a JOIN statement as opposed to selecting from both tables the way you are?
What's the output of the following query :
SELECT
TS.Day
,TS.Am_Pm
,TS.SessionId
FROM Table_Session TS
INNER JOIN Table_Booked TB ON TS.SessionId = TB.SessionID
OR TS.SessionId = 9
WHERE TB.Username = 'G9rpenman'
Could you post some sample data from both tables so we can see what you're trying to compare to provide more help?

DLOOKUP vs Multiple SELECT queries

I need help writing a query that will look up contact names multiple times.
I have two database tables...tblCONTACTS and tblPOLICIES.
tblCONTACTS has two fields, ID and CONTACTNAME. tblPOLICIES has 4 fields POL_ID, OWNER, ASSIGNEE, BENEFICIARY. Other than POL_ID, the other 3 fields are references to ID in the table tblCONTACTS.
How do I write a query to select all of the records in tblPOLICIES, but return the names from tblCONTACTS (instead of the ID that is contained in that field)?
Sorry if this is too amateur for this forum? I think I can do this with a Dlookup function in MS Access but am worried about performance. My other solution was to write multiple queries and the string the multiple queries together.
Thanks in advance.
SELECT
a.POL_ID,
b.CONTACTNAME as OWNER,
c.CONTACTNAME as ASSIGNEE,
d.CONTACTNAME as BENEFICIARY
FROM Test.tblPOLICIES as a
LEFT JOIN Test.tblCONTACTS as b ON a.OWNER = b.ID
LEFT JOIN Test.tblCONTACTS as c ON a.ASSIGNEE = c.ID
LEFT JOIN Test.tblCONTACTS as d ON a.BENEFICIARY = d.ID;
Will give you this result:
Regards

Mysql join query doesn't work for many to many relationship

I have 3 tables called
_partnership,
_partners,
_partnership_arm._partners = stores basic partner information
_partnership_arm = stores partnership arm details
_partnership = stores partners partnership records which includes the partner_id
arm_id which reference _partners.partner_id and _partnership_arm.arm_id.
So as an admin i want to select all details from the _partnership table which join other table reference without a where clause, but am having issue doing it.
here is my code
SELECT
_partnership.*,
_partners.names,
_partnership_arm.arm_name
FROM
`_partnership`
JOIN
`_partners`,`_partnership_arm` ON
_partnership.partner_id = _partners.partner_id
AND
_partnership.arm_id = _partnership_arm.arm_id
I also want a user to be able to select using a where clause
Please how can i achieve this?
Thank you.
SELECT
_partnership.*,
_partners.names,
_partnership_arm.arm_name
FROM
`_partnership`
JOIN
`_partners` ON _partnership.partner_id = _partners.partner_id
JOIN
`_partnership_arm` ON _partnership.arm_id = _partnership_arm.arm_id

MYSQL : Problem in writing where clause according to scenario

I have a DB (user_interests) set up with 3 fields: i_id (unique), interest_id, uid.
Then a second DB (interests) set up with the interests: interest_id (unique), interest_name
I'd like to do an SQL query to return a list of interests that two users have in common: User A (owner of a profile) and user B (you/viewer of a profile). I guess I need to query user_interests, then JOIN interests to get the name of the interest.
SELECT user_interests.i_id, user_interests.uid, interests.interest_name
FROM databases.user_interests
LEFT JOIN databases.interests
ON interest.interest_id = user_interest.interest_id
WHERE _______________
I'm confused about the where clause (if that is the correct way to do it at all). My goal is to get the interest_id from user_interests.interests where user_interests.uid is both A and then B (in separate rows).
I saw this link, but couldn't figure out what exactly I was missing: Group by x where y = A and B and C
I would solve it by joining two copies of user_interests, one which is filtered for user A (the profile owner), and one for user B, (the profile viewer).
SELECT *
FROM interests I
INNER JOIN user_interests A ON
A.interest_id = I.interest_id
AND A.user_id = {profile owner}
INNER JOIN user_interests B ON
B.interest_id = I.interest_id
AND B.user_id = {profile viewer}
Alternatively, more along the lines of the snippet you provided, you could complete the where clause with something like...
SELECT * FROM interests
WHERE interest_id in (SELECT interest_id
FROM users
WHERE user_id = A)
AND
interest_id in (SELECT interest_id
FROM user_interests
WHERE user_id = B)
Hope one of those works for you! Let me know if I can clarify
I don't think you need the where clause in this case just remove it and you will get the set of data you are looking for:
SELECT user_interests.i_id, user_interests.uid, interests.interest_name
FROM databases.user_interests
LEFT JOIN databases.interests
ON interest.interest_id = user_interest.interest_id
You may also create a where statement such as the following if you are looking to get a specific result set. I'm not discrediting the answer previously submitted, I am simply trying to help you with the specific WHERE statement you're looking for.
SELECT user_interests.i_id, user_interests.uid, interests.interest_name
FROM databases.user_interests
LEFT JOIN databases.interests
ON interests.interest_id = user_interests.interest_id
WHERE user_interests.uid IN ('A','B');
Please also note, that I changed your ON join to use interests and user_interests, with 's' appended to both, as those are the names of the table. They maintain the same schema name as they are assigned in the database.
Your query is correct remove the where part and run it. It will give you the same output as you need...

Database design question - Performance needed

I created an inventory system which mostly is using a server-sided scripting language to do all the work. To try and get some performance gains I am looking to better design my database to try and minimizing the scripts.
I have a table named metal_part which has a one to one relationship with five other tables, basically the other tables are other parts, which those parts then have a one to one relationship with a few other tables.
When I query metal_part I need all the UPC numbers from each table, so its direct one to one relationships need to get their own information from their direct one to one relationship tables ect... Is it possible to make a huge query to build it all and put it in a form at like:
(###) - ####/##/##/## [a-z]
Using a query? or do I have to get all the information and concat it using a scripting language?
Thanks
You should be able to get all of the information you need using a standard join, and then, with the concat function appropriate to your database (see here http://www.1keydata.com/sql/sql-concatenate.html) you can form the string you want.
Your question is very vague.
I'm guessing you are talking about matching on a primary key called partnumber or something like that.
You can do this using a query like
SELECT mp.partnumber
, mp.UPC_number
, wp.UPC_number
, pp.UPC_number
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127';
You can also do
SELECT mp.partnumber
, group_concat(mp.UPC_number) as metal_UPCs
, group_concat(wp.UPC_number) as wood_UPCs
, group(concat(pp.UPC_number) as plastic_UPCs
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;
or
SELECT mp.partnumber
, concat_ws(','
, group_concat(mp.UPC_number)
, group_concat(wp.UPC_number)
, group(concat(pp.UPC_number)
) as UPCs_of_parts
FROM metal_parts mp
INNER JOIN wood_parts wp ON (wp.partnumber = mp.partnumber)
INNER JOIN plastic_parts pp ON (pp.partnumber = mp.partnumber)
WHERE mp.partnumber = '8874578127'
GROUP BY mp.partnumber;