Multiple column SQL joins in a table - asp.net

Background:
Hi there, first time posting a question so please excuse any wrongdoings...
I have been given an assignment at university using ASP.NET and SQL Server, what we basically have to do is create a webform where we can select a political party via a radio button list bound to a SQL data source. Based on this selection, a drop-down list is then populated with a list of MPs who are a part of the party selected the radio button list. You can then select an MP and view his or her personal details, their roles in parliament and ask/view questions via a number of data controls linked to various SQL data sources.
I'm making good progress and trying to use whatever resources I can find to get solutions but alas I've hit a wall with viewing the parliamentary roles after selecting the MP.
I have 3 tables which are involved here:
MP MP_ROLES ROLE
------------- --------------- ----------------
MP_NUM (PK) MP_NUM (PK, FK) ROLE_NUM (PK)
MP_FIRST ROLE_1 (FK) ROLE_DESCRIPTION
MP_LAST ROLE_2 (FK)
MP_EMAIL ROLE_3 (FK)
MP_CONTACTADD ROLE_4 (FK)
MP_PHOTO
PARTY_NUM(FK)
(where ROLE_1 through ROLE_4 reference ROLE.ROLE_NUM.)
Problem:
I have a DetailsView control which is bound to an SQL data source which queries MP_ROLES. What I'm basically trying to do is replace each value in ROLE_1, ROLE_2, etc. with the corresponding ROLE_DESCRIPTION value referenced by ROLE_NUM in the ROLE table (as MP_ROLE.ROLE_1 = ROLE.ROLE_NUM AND MP_ROLE.ROLE_2 = ROLE.ROLE_NUM AND MP_ROLE.ROLE_3 = ROLE.ROLE_NUM AND MP_ROLE.ROLE_4 = ROLE.ROLE_NUM). Now through my digging through textboxes I know in more simple applications you can do what I'm trying to do with the INNER JOIN function, however as I'm still learning I haven't quite got the syntax sorted to do what I want to do.
What I have so far:
SELECT MP_ROLES.MP_NUM, MP_ROLES.ROLE_1, MP_ROLES.ROLE_2, MP_ROLES.ROLE_3, MP_ROLES.ROLE_4
FROM MP_ROLES
INNER JOIN ROLE ON MP_ROLES.ROLE_1 = ROLE.ROLE_NUM
INNER JOIN ROLE AS ROLE_3 ON MP_ROLES.ROLE_2 = ROLE.ROLE_NUM
INNER JOIN ROLE AS ROLE_2 ON MP_ROLES.ROLE_3 = ROLE.ROLE_NUM
INNER JOIN ROLE AS ROLE_1 ON MP_ROLES.ROLE_4 = ROLE.ROLE_NUM
WHERE (MP_ROLES.MP_NUM = #MP_NUM)
I've been hung up on this for the last 8 trying to find a solution so if there is anyone who is able show me where I'm going wrong or perhaps kind enough to provide a solution it would be very much appreciated :)

Your problem lies in the = ROLE.ROLE_NUM. You have to specify the alias here too. Also I rearranged some numbers since your numbers were all mixed up.
SELECT MP_ROLES.MP_NUM
, MP_ROLES.ROLE_1
, MP_ROLES.ROLE_2
, MP_ROLES.ROLE_3
, MP_ROLES.ROLE_4
FROM MP_ROLES
INNER JOIN ROLE AS ROLE_1 ON MP_ROLES.ROLE_1 = ROLE_1.ROLE_NUM
INNER JOIN ROLE AS ROLE_2 ON MP_ROLES.ROLE_2 = ROLE_2.ROLE_NUM
INNER JOIN ROLE AS ROLE_3 ON MP_ROLES.ROLE_3 = ROLE_3.ROLE_NUM
INNER JOIN ROLE AS ROLE_4 ON MP_ROLES.ROLE_4 = ROLE_4.ROLE_NUM
WHERE (MP_ROLES.MP_NUM = #MP_NUM)

It looks as though what I was actually aiming for can be done using the LEFT OUTER JOIN function:
SELECT
MP_ROLES.MP_NUM,
MP_ROLES.ROLE_1,
MP_ROLES.ROLE_2,
MP_ROLES.ROLE_3,
MP_ROLES.ROLE_4,
ROLE1.ROLE_NUM, ROLE1.ROLE_DESCRIPTION AS POSITION1,
ROLE2.ROLE_NUM, ROLE2.ROLE_DESCRIPTION AS POSITION2,
ROLE3.ROLE_NUM, ROLE3.ROLE_DESCRIPTION AS POSITION3,
ROLE4.ROLE_NUM, ROLE4.ROLE_DESCRIPTION AS POSITION4
FROM MP_ROLES
LEFT OUTER JOIN ROLE AS ROLE1 ON MP_ROLES.ROLE_1 = ROLE1.ROLE_NUM
LEFT OUTER JOIN ROLE AS ROLE2 ON MP_ROLES.ROLE_2 = ROLE1.ROLE_NUM
LEFT OUTER JOIN ROLE AS ROLE3 ON MP_ROLES.ROLE_3 = ROLE1.ROLE_NUM
LEFT OUTER JOIN ROLE AS ROLE4 ON MP_ROLES.ROLE_4 = ROLE1.ROLE_NUM
WHERE (MP_ROLES.MP_NUM = #MP_NUM)

Related

how to use join with Sqlite Index

I am creating a app with ionic 3. In the app I added a search filter which filter users according to their role. I have 5 tables from which I have to pull the data.In term to do so, I did these steps..
created each table index with the required data.
CREATE INDEX IF NOT EXISTS AccountIndexTable ON Accounts(id,roleId,firstName,lastName,email,accountId);
CREATE INDEX IF NOT EXISTS AddressIndexTable ON Addresses(id,addressTypeId,street,city,state,country,pincode,accountId)
CREATE INDEX IF NOT EXISTS CompanyIndexTable ON Companies(id,name,accountId)
CREATE INDEX IF NOT EXISTS CommunicationIndexTable ON Communications(id,phone,accountId)
CREATE INDEX IF NOT EXISTS OptionalInfoIndexTable ON CustomerOptionalInfos(id,customerType,accountId)
and then i used join to get data. Before that I have questions
1: Do I need to use the indexed table in my join?
the Join query:
SELECT Accounts.accountId AS accountID,
Accounts.roleId AS roleID,
Accounts.email AS email,
Accounts.firstName || " " || Accounts.lastName AS name,
Addresses.street AS street,
Addresses.city AS city,
Addresses.state AS state,
Addresses.country AS country,
Addresses.pincode AS pincode,
Communications.phone AS phone,
Companies.name AS compName,
CustomerOptionalInfos.customerType AS cType
FROM Accounts
LEFT JOIN Addresses ON Addresses.accountId=Accounts.accountId
AND Addresses.addressTypeId=1
LEFT JOIN Communications ON Communications.accountId=Accounts.accountId
LEFT JOIN Companies ON Companies.accountId=Accounts.accountId
LEFT JOIN CustomerOptionalInfos ON CustomerOptionalInfos.accountId=Accounts.accountId
WHERE 1=1
AND Accounts.isDelete!='true'
AND Accounts.firstName!= ''
AND Accounts.roleId = 5
ORDER BY Accounts.firstName ASC
LIMIT ?,?
It still takes +2 seconds to execute. Please suggest me the better way of doing this. I have a large number of data.

SQL to DQ, use sub select

I'm trying to convert a SQL function to a DQL function.
I don't know how to do the following step:
Join on multiple class
Join on temporary table (created by doctrine)
Call a sub select in my function
Here my actual working SQL code.
SELECT ut.token
FROM user_token ut
INNER JOIN factory f ON f.id = ut.factory_id
INNER JOIN article_factory af ON af.factory_id = f.id
INNER JOIN article a ON a.id = af.article_id
WHERE ut.factory_id = (
SELECT f.id
FROM factory f
INNER JOIN article_factory af ON af.factory_id = f.id
INNER JOIN article a ON a.id = af.article_id
WHERE a.id = :article
)
GROUP BY ut.token
In case of, here a screen of my table relation.
article_factory is not an entity or a class. article_factory was created for my relation between article and factory, when I specified a ManyToMany relation.
EDIT: Tryed throught plain SQL, but got issue making link between article and fatory, since DQL only know entity, and Doctrine is creating a table, which I canno't use. With DQL, I still got the same issue than above, I don't know how to properly join my classes, and make my sub-select.

How to use left outer join in my sql query?

Here is my query:
ALTER PROCEDURE sp_logdetails
(
#bookid INT
)
AS
BEGIN
SELECT *
FROM book_lending
LEFT OUTER JOIN student ON student.id = book_lending.id
WHERE bookid = #bookid
END
When i execute above query, it shows only book_lending data, not the student data. Here is my screenshot
And it is my student table data screenshot:
May i know, how to get student data in which particular bookid. I used to set foreign key for book_lending id to student id.
Can anyone guide me to fix this?
Thanks,
Select the specific columns from joined table in your select list. Here bl and s is table alias for better redability. You need to select the columns you want and include them to your select list. below query will select all columns from both tables.
SELECT bl.*, s.*
FROM book_lending bl
LEFT OUTER JOIN student s ON s.id = bl.id
AND bl.bookid = #bookid
You got the fields mixed up in the join:
LEFT OUTER JOIN student ON student.id = book_lending.id
You try to match a student to book lending by ID of both - which is wrong.
Change the code to this and you will start getting results back:
SELECT *
FROM book_lending
LEFT OUTER JOIN student ON student.id = book_lending.studentid
WHERE bookid = #bookid

SQLITE : inline views or nested subqueries?

I'm trying to write a query on two simple tables. Tables are simple, the query is not :)
Anyway...
Here is the database scheme :
and here is an overview of table content :
I'm trying to write a query that would list all assets in corresponding table, only if the are marked as "wanted" (meaning the boolean field asset_owned =0) and that are referenced for another owner as "owned".
This is what I have so far and it works :
SELECT
user.user_pseudo AS REQUESTER,
asset.asset_sku AS SKU,
asset.asset_name AS ASSET_NAME
FROM
asset
INNER JOIN user ON asset.id_user = user.id
WHERE
asset.asset_owned = 0
AND
asset.asset_sku IN (SELECT asset.asset_sku FROM asset WHERE asset.asset_owned = 1)
But, in the same query (if possible) I would like to get the owner name as well.
The first result of such a query on those table would be :
me,003,Test003,you.
I've tried inline SELECT and nested subqueries like :
SELECT
user.user_pseudo as ASKER,
asset.asset_sku as SKU,
asset.asset_name as NAME,
subquery1.user.user_pseudo as OWNER
FROM
asset
INNER JOIN user ON asset.id_user = user.id,
(SELECT user.user_pseudo.asset_asset_sku FROM asset INNER JOIN user ON asset.id_user = user.id WHERE asset.asset_owned = 1) subquery1
WHERE
asset.asset_owned = 0 AND
subquery1.asset.asset_sku IN (SELECT asset.asset_sku FROM asset INNER JOIN user ON asset.id_user = user.id WHERE asset.asset_owned=1)
but of course that does not work.
Thanks for any direction you could point me to.
happy new year
Mathias
So this was fun for me (I'm learning SQL, so this is good practice!) - I appreciate the very clear question.
Hopefully this works for you - I used two sub-queries (one each for 'owner' and 'requester') and then joined those on SKU and name. It works in SQLite with the small sample data shown above.
SELECT requester, subq1.SKU, subq1.name, owner
FROM
(SELECT pseudo AS requester, SKU, name
FROM asset, user
WHERE owned = 0
AND user.id = id_user) subq1,
(SELECT pseudo AS owner, SKU, name
FROM asset, user
WHERE owned = 1
AND asset.id_user = user.id) subq2
WHERE subq1.SKU = subq2.SKU
AND subq1.name = subq2.name;

MS Access - Run a query displaying one result per user

I've got a database that I'm tying into our access control system. I'm trying to create a report showing the user's first entry and last exit from the building. I've put a query together which hows me the right single result for one user, but I want to automate this to run through a list of users.
The query for grabbing the first entry for one user (as it is currently) is as follows:
SELECT TOP 1 AccessLog.ID, AccessLog.Date, First(AccessLog.Time) AS FirstOfTime, AccessLog.User, AccessLog.Details, AccessLog.Event, AccessLog.Department, AccessLog.Where
FROM AccessLog
GROUP BY AccessLog.ID, AccessLog.Date, AccessLog.User, AccessLog.Details, AccessLog.Event, AccessLog.Department, AccessLog.Where
HAVING (((AccessLog.Date)=#DATE#) AND ((AccessLog.User)="USERNAME") AND ((AccessLog.Where)="Front Door (In)"))
ORDER BY First(AccessLog.Time);
I have a separate table of all the usernames, would it be possible to do a run-as for this?
Basically I'm a complete Access n00b, in the past I'd have done this with PHP and a recurring function but this should be fairly easy to achieve within Access, shouldn't it? Any help much appreciated.
You can get a list of the arrival times for all users by date with the following query. Save the query definition under the name "ArriveTimes":
SELECT AccessLog.Date, AccessLog.User, Min(AccessLog.Time) AS ArriveTime
FROM AccessLog
WHERE (((AccessLog.Where)="Front Door (In)"))
GROUP BY AccessLog.Date, AccessLog.User;
Similarly, you can get the departure times with a similar query saved as "DepartTimes":
SELECT AccessLog.Date, AccessLog.User, Max(AccessLog.Time) AS DepartTime
FROM AccessLog
WHERE (((AccessLog.Where)="Front Door (Out)"))
GROUP BY AccessLog.Date, AccessLog.User;
Then you can join the [ArriveTimes] and the [DepartTimes] queries together (JOIN on [Date] and [User]) and pull the other details by joining on two instances of the [AccessLog] table (JOIN on [Date], [User], and [Time]):
SELECT ArriveTimes.Date, ArriveTimes.User,
ArriveTimes.ArriveTime, AccessLog_A.Details AS ArriveDetails,
DepartTimes.DepartTime, AccessLog_D.Details AS DepartDetails
FROM
(AccessLog AS AccessLog_A INNER JOIN
(ArriveTimes INNER JOIN DepartTimes
ON (ArriveTimes.User = DepartTimes.User)
AND (ArriveTimes.Date = DepartTimes.Date)
)
ON (ArriveTimes.ArriveTime = AccessLog_A.Time)
AND (AccessLog_A.User = ArriveTimes.User)
AND (AccessLog_A.Date = ArriveTimes.Date)
)
INNER JOIN AccessLog AS AccessLog_D
ON (DepartTimes.DepartTime = AccessLog_D.Time)
AND (DepartTimes.User = AccessLog_D.User)
AND (DepartTimes.Date = AccessLog_D.Date);

Resources