SQL: Matching multiple columns value - sql

I have a database set up so that the username (username) and id (id) are stored in the members table.
I have another table that records reports and I record each column in the table (fid), who reported it (rid) and who they were reporting (id) which both match to the user's id in the members table.
How could I get a query to pull the username for both the rid and id?
My current query is
SELECT selfreport.fid, selfreport.rid,
selfreport.id, members.username as username
FROM members, selfreport
WHERE members.id = selfreport.id
ORDER BY fid
but this only gets the username for who they were reporting. How can I get it to pull the username for both?

You need to join to your members table twice. Try something like this:
SELECT selfreport.fid,
selfreport.rid,
selfreport.id,
COALESCE(WhoReported.username, 'Not Specified') AS WhoReportedUN,
COALESCE(ReportedTo.username, 'Not Specified') AS ReportedToUN
FROM selfreport
LEFT JOIN members WhoReported ON WhoReported.id = selfreport.id
LEFT JOIN members ReportedTo ON ReportedTo.id = selfreport.rid
ORDER BY fid

Do not use implicit SQL '89 joins they are an antipattern.
Use explicit join syntax instead.
SELECT s.fid, s.rid, s.id, m1.username as username, m2.username as rusername
FROM selfreport S
INNER JOIN members m1 ON (m1.id = s.id)
INNER JOIN members m2 ON (m2.id = s.rid)
ORDER BY s.fid
If id or rid is optional, use a left join.
SELECT
s.fid, s.rid, s.id
, COALESCE(m1.username, 'nobody') as username
, COALESCE(m2.username, 'nobody') as rusername
FROM selfreport S
LEFT JOIN members m1 ON (m1.id = s.id)
LEFT JOIN members m2 ON (m2.id = s.rid)
ORDER BY s.fid

You need to join members twice:
SELECT selfreport.fid,
selfreport.rid,
selfreport.id,
m1.username AS ReportToUsername,
m2.username AS ReporteeUsername
FROM selfreport
INNER JOIN members m1
ON m1.id = selfreport.id
INNER JOIN members m2
ON m2.id = selfreport.rid
ORDER BY fid
Since you were doing an implicit join in your original query, I believe INNER JOIN will suit you well. However, if it's possible to have null values in selfreport.id or selfreport.rid, you should use LEFT JOIN instead.

Related

Get all the values from the first left table but when two left joins used its restricting the values from first table

I am trying to get all the values from the first left table but when I use two left joins its restricting the values from first table.
I used the below query
SELECT P.person_id, TS.Task_Id, TS.skill
FROM Person P
LEFT JOIN Person_Skill PS ON P.person_id = PS.person_id
LEFT JOIN Task_Skill TS ON PS.Skill = TS.Skill
WHERE ts.task_id = 245
I need all the person id from person table.
Just move the condition on the left joined table from the where clause to the on clause of the join:
select p.person_id, ts.task_id, ts.skill
from person p
left join person_skill ps
on p.person_id = ps.person_id
left join task_skill ts
on ps.skill = ts.skill
and ts.task_id = 245 --> here
Rationale: conditions in the where clause are mandatory. If there is no match in ts, then condition ts.task_id = 245 cannot be satisfied, since ts.task_id is null.
Use the filter condition in a sub query instead of using it as a global filter outside. This should give you the output that you desire.
SELECT P.person_id,TS.Task_Id,TS.skill FROM Person P
LEFT JOIN Person_Skill PS
ON P.person_id=PS.person_id
LEFT JOIN
(Select * from Task_Skill where task_id = 245) TS
ON PS.Skill=TS.Skill;

how can i access column from subquery

select u.phone, u.email , t.to_address (error from this)
from user_accounts u
where u.id
in
(select w.user_id
from wallets w
where w.id
in
(
select t.wallet_id
from withdraws t
where t.to_address
in
('1F6o1fZZ7', 'pJDtRRnyhDN')))
I want to get the column to_address from subquery. How can I get it in postgresql?
I try assign 'AS' for subquery but it didn't work
A join returns a result table constructed from data from multiple tables. You can also retrieve the same result table using a sub query. A sub query is simply a SELECT statement within another select statement.
select u.phone, u.email , t.to_address (
from user_accounts u
INNER JOIN wallets w ON u.id= w.user_id
INNER JOIN withdraws t ON t.wallet_id =w.id
where t.to_address in ('1F6o1fZZ7', 'pJDtRRnyhDN')
use join with all the table, you dont need any subquery
select u.phone, u.email , ww.to_address
from user_accounts u left join wallets w on u.id=w.user_id
left jon withdraws ww on w.id=ww.wallet_id
where ww.to_address in ('1F6o1fZZ7', 'pJDtRRnyhDN')
You can not access t.address because that column inside in condition.
I used left join but it seems it will be inner join type because you used filter in ('1F6o1fZZ7', 'pJDtRRnyhDN') though after applying where condition it also behave like inner join
You cannot achieve what you're trying using subquery. When you want records from different tables and they have a unique column in common that connects them then You should do it using a JOIN.
Sometimes (Not all cases) IN can cause performance problems, so you should consider knowing more about different types of JOINS(https://www.w3schools.com/sql/sql_join.asp)
Check the link for comparison:
Inner join versus doing a where in clause
About the Query:
SELECT
u.phone, u.email , t.to_address (error from this)
FROM
user_accounts u
INNER JOIN wallets w ON u.id = w.id
INNER JOIN withdraws t ON t.wallet_id = w.id
WHERE
t.to_address IN ('1F6o1fZZ7', 'pJDtRRnyhDN')

SQL List all of MemberID's of people who arent in the ones listed

Outputted Date
I have a seperate Members table which has all the members ID's and I want to list all those but get rid of the ones that are displayed in this list.
SELECT DISTINCT tbl_classregistration.ClassID, tbl_classregistration.MemberID
FROM tbl_member INNER JOIN (tbl_classes
INNER JOIN tbl_classregistration ON
tbl_classes.ClassID = tbl_classregistration.ClassID) ON
tbl_member.MemberID = tbl_classregistration.MemberID
GROUP BY tbl_classregistration.ClassID, tbl_classregistration.MemberID
HAVING (((tbl_classregistration.ClassID)=[Enter ClassID]));
Thats the SQL View
Use not in:
select memberid from members where memberid not in (SELECT DISTINCT tbl_classregistration.MemberID
FROM tbl_member INNER JOIN (tbl_classes INNER JOIN tbl_classregistration ON tbl_classes.ClassID = tbl_classregistration.ClassID) ON tbl_member.MemberID = tbl_classregistration.MemberID
GROUP BY tbl_classregistration.ClassID, tbl_classregistration.MemberID
HAVING (((tbl_classregistration.ClassID)=[Enter ClassID])))

how can I get the selected columns fully and the sum column separately

SELECT f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date,sum(payments.paid_amount)
FROM payments
LEFT JOIN family ON family.id = payments.family_id
LEFT JOIN teachers ON family.teacher_id = teachers.t_id
How can I get the selected columns fully and the sum column separately?
because that sum function makes all the selected result one row
SELECT f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date
FROM payments
LEFT JOIN family ON family.id = payments.family_id
LEFT JOIN teachers ON family.teacher_id = teachers.t_id
This query is working fine without the sum column
You didn't tell the database, which column to use for aggregating the data. Don't know which database you are using, but some complain, that there is no GROUP BY statement in the SQL text.
Please try with the following query:
SELECT f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date,sum(payments.paid_amount)
FROM payments
LEFT JOIN family ON family.id = payments.family_id
LEFT JOIN teachers ON family.teacher_id = teachers.t_id
GROUP BY f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date
GROUP BY tells the database, which are the key columns in the aggregation.
If you want all the payments, use a subquery or join:
SELECT f_name, l_name, t.first_name, t.t_id, p.p_id, p.paid_amount, p.family_id, date,
(select sum(p.paid_amount) from payments) as all_paid
FROM payments p LEFT JOIN
family f
ON f.id = p.family_id LEFT JOIN
teachers t
ON f.teacher_id = tetchers.t_id;
SELECT f_name,l_name,t.first_name,t.t_id,p_id,paid_amount,family_id,date,sum(p.paid_amount)
FROM payments p,family f,teachers t where f.id = p.family_id and f.teacher_id = t.t_id
Group by f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id
You can add date column also in Group by expression based on your requirement. Example:
f_name,l_name,teachers.first_name,teachers.t_id,p_id,paid_amount,family_id,date

join using two column in sql

ProjectMaster
ProjectID ITManagerID DevelopmentManagerID
--------------------------------------------------
1 1000 1001
UserTable
UserID UserName
----------------
1000 Sam
1001 Ram
Result
Project ItManagerName DevManagerName
------------------------------------------------
1 sam ram
Help to write query
Edit:
I tried
select
projectid,projectName,projectdescription,startdate,enddate,
apsmanagerid,projectmanager,ragstatus,projectstatus,projectpriority,
categoryid,inactivedate,comments,it.userName AS ITProjectManagerName,
dev.userName as DevManagerName
from pmis_project p,pmis_user It,pmis_user dev
where p.DevprojectManager = It.userid
and p.ITmanagerid = dev.userid
and p.projectid IN (Select Projectid from SelectedProject)
You can JOIN a table as many times as needed.
In this case
one JOIN to Users to get the ITManager's name.
one JOIN to Users to get the DevManager's name.
SQL Statement
SELECT ProjectID
, ITManagerName = m.UserID
, DevManagerName = d.UserID
FROM ProjectMaster pm
INNER JOIN UserTable m ON m.UserID = pm.ITManagerID
INNER JOIN UserTable d ON d.UserID = pm.DevelopmentManagerID
You can include the same table multiple times in the FROM clause of a SELECT query. If you're doing this, you frequently want to introduce an alias for one or more of these tables, so that they can be referred to in the rest of the query. You introduce an alias by adding AS alias after the name of the table, e.g.:
SELECT
*
FROM
Table As t
WHERE
t.Column = 'x' --<-- using the alias here
(the AS is actually optional)
Joining tables in a FROM clause is performed by using the JOIN keyword, and placing the conditions for the join in the ON:
FROM
Table1 t1
inner join
Table2 t2
on
t1.ColumnA = t2.ColumnB
SELECT * FROM ProjectMaster PM
INNER JOIN UserTable UT ON UT.UserId = ITManagerId
INNER JOIN UserTable UT1 ON UT1.UserId = DevelopmentManagerId