writing a query that contains two constraints on the same table - sql

I am aware that I am asking something that may look trivial but I could not find and answer or a duplicate. (I am new with this area).
So i am trying.
I have two tables
Tests table with the following columns
1. storyID , authorID , assigneeID
2. userID , userName
I would like to write a query that return storyID, UserName of author ID, UserName of assigneeID.

SELECT storyID, u1.userName AS authorName, u2.userName AS assigneeName
FROM storytable s
INNER JOIN usertable u1 ON s.authorID = u1.userID
INNER JOIN usertable u2 ON s.assigneeID = u2.userID
The above solution will only work in case you have relation between userID, assigneeID and authorID fields.

Select and create an alias for the second table.
SELECT a.storyID, b.userName, c.userName
FROM tablea a
INNER JOIN tableb b ON a.authorID = b.userID
INNER JOIN tableb c ON a.assigneeID= c.userID

Assuming the authorID = UserId...
select
1.storyId,
author.userName,
assignee.userName
from 1
inner join 2 as author
on 1.authorId = 2.userId
inner join 2 as assignee
on 1.assigneeID = 2.userID

Related

SQL QUERY ON 2 TABLES WITH EXCLUSION

I'm looking for a sql query to select IDs from the "product" table for "user" 1 that don't appear in the "Fake" table in the "product_id" field for "customer _id" 20 (for example). I hope to be clear enough in my request. Thanks for your help
Thanks XQbert. I think I succeeded thanks to your advice. Thank you
SELECT P.*, U.name
FROM product P
INNER JOIN user U
ON P.user_id = U.id
WHERE U.id= '1'
AND NOT EXISTS
(SELECT F.*
FROM fake F
INNER JOIN customer C
ON F.customer_id = C.id
WHERE P.id = F.product_id
AND C.customer_id = 20)

How do I match against multiple conditions on a table join?

I have two tables:
users attributes
id|name id|name|user_id
------- ---------------
1 |foo 1 |bla | 1
2 |bar 1 |blub| 1
1 |bla | 2
How do I create a query gives users with both the "bla" AND "blub" attributes?
In this case it should only return the user "foo".
I know that the data is not normalized.
SELECT u.*, a.id, b.Id, a.name, b.name FROM users u
JOIN attributes a
ON a.User_id = u.User_id AND a.name = 'bla'
JOIN attributes b
ON u.User_Id = b.User_id AND b.name = 'blub'
Assuming an attribute association to a user is unique...
if you need 3 conditions to be true add the conditions to the in and adjust count up 1.
SELECT u.name
FROM users u
INNER JOIN attributes a on A.user_Id = u.id
WHERE a.name in ('bla','blub')
GROUP by u.name
HAVING count(*)=2
and if you don't have an unique association, or you need to join to another table you could always do...
SELECT u.name
FROM users u
INNER JOIN attributes a on A.user_Id = u.id
WHERE a.name in ('bla','blub')
GROUP by u.name
HAVING count(distinct A.name)=2
for a slight performance hit. but this allows you to join and get back additional fields which others have indicated was a detriment to this method.
This allows for scaling of the solution instead of incurring the cost of joining each time to different tables. In addition, if you needed thirty-something values to associate, you may run into restrictions on the number of allowed joins.
SELECT U.NAME
FROM USERS U
INNER JOIN
ATTRIBUTES A1
ON U.ID = A1.USER_ID
INNER JOIN
ATTRIBUTES A2
ON U.ID = A2.USER_ID
WHERE A1.NAME = 'bla'
AND A2.NAME = 'blub'
You can use the INTERSECT operator
SELECT
u.id
,u.name
FROM users AS u
INNER JOIN attributes AS a
ON u.id = a.user_id
WHERE a.name = 'bla'
INTERSECT
SELECT
u.id
,u.name
FROM users AS u
INNER JOIN attributes AS a
ON u.id = a.user_id
WHERE a.name = 'blub'
;
Here is a demo on SQL Fiddle: http://sqlfiddle.com/#!6/68986/5
More info on SET operations in SQL: http://en.wikipedia.org/wiki/Set_operations_(SQL)
SELECT u.name
FROM attributes a
JOIN users u
ON u.id = a.user_id
WHERE a.name IN ('bla','bulb')

List all friends for a userid SQL query

I have 2 tables a user table (user_id, fname, lname, dob, etc) and a are_friends table
(userA_id, userB_id). I have been trying to do this query for a while now, I need it to list all friends for a user_id.
What I have got so far,
SELECT
U.user_id,
U.fname,
U.lname
FROM are_friends A, user U
WHERE
A.user_id = U.user_id
AND (
A.user_id = 1
OR A.user_id IN (SELECT userB_id FROM are_friends WHERE userA_id = 1)
);
Any help will be much appreciated.
Try using an INNER JOIN like this:
SELECT u2.user_id, u2.fname, u2.lname
FROM user u
INNER JOIN are_friends f ON f.userA_id = u.user_id
INNER JOIN user u2 ON u2.user_id = f.userB_id
WHERE u.user_id = 1
You can change the WHERE clause to specifically get the friends of another user id.

sql optimize search many-many

usertable
----
id, username
grouptable
----
id, groupname
group_user
--------
uid, gid
books
----
id, groupname
Input parameters: groupname, username
output: list of books
Is it possible to use 1 sql statement to get list of books when username is inside groupname
Question 2: any good book to recommand to master complex sql statement..
This query gives list of books by user parametar
SELECT b.id,
FROM usertable u
INNER JOIN group_user gu ON gu.uid = u.id
INNER JOIN grouptable g ON g.id= gu.gid
INNER JOIN books b ON b.groupname = g.groupname
WHERE u.username = #user_name
Also i think if you have the group name you can use
SELECT b.id,
FROM grouptable g
INNER JOIN books b ON b.groupname = g.groupname
WHERE g.groupname = #group_name
Bus having select on both parameters i think is not very good think. This query will get
list of books for user group name
SELECT b.id,
FROM usertable u
INNER JOIN group_user gu ON gu.uid = u.id
INNER JOIN grouptable g ON g.id= gu.gid
AND g.group_name = #group_name
INNER JOIN books b ON b.groupname = g.groupname
WHERE u.username = #user_name

how to combine 2 or more bridge tables in a single query

i have the following tables:
Table: People (id, first, last, age, phone, etc . .)
Table: Roles (id, name)
Table: Skills (id, name)
Table: People_Roles (id, personID^, roleID^)
Table: People_Skills (id, personID^, skillID^)
^ = foreign key
I basically want a query that gives me the full result set of all people and their roles and there skills.
Person.First, Person.Last, Roles.Name, Skills.Name
SELECT p.first, p.last, r.name, s.name
FROM People p
LEFT JOIN People_Roles pr
ON pr.personID = p.id
INNER JOIN Roles r
ON pr.roleID = r.id
LEFT JOIN People_Skills ps
ON ps.personID = p.id
INNER JOIN Skills s
ON ps.skillID = s.id
This query will select you all the people, even those without Roles or Skills assigned.
This query will work:
SELECT Person.First, Person.Last, Roles.Name, Skills.Name
FROM People
LEFT JOIN People_Skills
ON People.id = People_Skills.personID
INNER JOIN Skills
ON People_Skills.skillID = Skills.id
LEFT JOIN People_Roles
ON People.id = People_Roles.personID
INNER JOIN Roles
ON People_Roles.roleID = Skills.id
By the way, why your "bridge" tables have their own ID. I would just use a compound key made of PersonID and the corresponding foreign key (skillsID or rolesID) to ensure that every pair can only be made once.