Default values when NULL - sql

related to this answered question I asked yesterday Recursive query where anchor and member have unions
I have a junction table that contains
menuItem | roleID | userID
1 | 2 | NULL
1 | 5 | NULL
2 | NULL | 81
I have a roles table
roleID | roleName
1 | admin
3 | super admin
5 | basic user
What I would like is when querying for the USERS who have access to this menu item to also include to the admin users (roleIDs 1 and 3). In the linked question I was able to include the admin users by including the IDs in the join clause. But because the roleID is null when including the individual users, I can't ever get the results to produce:
userName | menuItem
admin1 | 2
admin2 | 2
realUser | 2
At best i've been able to get
userName | menuItem
realUser | 2
realUser | 2
realUser | 2
How can I write this so the admin users are included as part of the result? thanks.

It sounds like you want to join a table Users to a table Menu
when
The user's Role ID is 1 or 3 and the menu role id is null
Or the userid in the user table matches the userid
Or the users's role id matches the menu's role id
So that looks like this
From
users u
INNER JOIN Menu m
ON (u.RoleID in (1,3) and m.roleID is Null)
or u.UserId = m.userId
or u.RoleId = m.RoleId

Related

DB2 SQL - Select all values of a table related to another table

Maybe I'm not clear on the topic for what I'm trying to ask, so here is my question:
I have three tables for now, say, User, User Role, Role, which make reference to each other:
User
========
USER_ID | USER_NAME
1 | User A
2 | User B
User Role
========
USER_ID | ROLE_ID
1 | 1
1 | 2
2 | 1
Role
========
ROLE_ID | ROLE_NAME
1 | Admin
2 | Supervisor
As you can see, User A has two role for now: Admin and Supervisor
What I'm trying to do now is selecting all users with role Admin, then the sql result should display all other roles related to that user also.
Say, the sql is executed like this:
select A.*, C.ROLE_NAME from User A, User Role B, Role C where A.USER_ID = B.USER_ID and B.ROLE_ID = C.ROLE_ID and B.ROLE_ID = '1'
The above sql should only display User A with only one role, Admin:
USER_ID | USER_NAME | ROLE_NAME
1 | User A | Admin
2 | User B | Admin
But what I want is, beside Admin, the sql result should also display User A with ROLE_NAME = Supervisor:
USER_ID | USER_NAME | ROLE_NAME
1 | User A | Admin
1 | User A | Supervisor
2 | User B | Admin
Any ideas?
Use exists:
with ur as (
select u.*, C.ROLE_NAME
from User u join
User_Role ur
on u.user_id = ur.user_id join
Role r
on r.role_id = ur.role_id
)
select ur.*
from ur
where exists (select 1
from ur ur2
where ur2.user_id = ur.user_id and ur2.role_name = 'admin'
);
Also, never use commas in the FROM clause. Always use proper, explicit JOIN syntax with the condition in the ON clause.
Second, use meaningful table aliases rather than arbitrary letters such as "a", "b", and "c".

Postgres Many to many mapping sql query

Postgresql Database
Table User
-----------
ID | Name
1 | John
2 | Bob
3 | Sarah
Table Photo
-------------
ID | Caption
1 | Vacation
2 | Birthday
3 | Christmas
Table Comment
--------------
ID | User ID | Photo ID| Text
1 | 1 | 1 | Mexico Looks Great
2 | 2 | 1 | Sure Does
3 | 3 | 1 | Too Hot
4 | 1 | 2 | Look at that cake
5 | 3 | 2 | No ice cream?
6 | 1 | 3 | So Happy
Desire: I want to get all the photos that ONLY John(1) and Sara(3) commented on.
How do I build a SQL query that looks for photos that only have comments from user #1 and user #3, I want to EXCLUDE results where more(or less) than those two commented on.
The clearest and most readable way, is the Photos containing comments by:
User1 Intersect User2 Except Any other user
This SQL Fiddle and query will return that:
SELECT *
FROM Photo
WHERE ID IN (
SELECT "Photo ID" FROM Comment WHERE "User ID" = 1
INTERSECT
SELECT "Photo ID" FROM Comment WHERE "User ID" = 3
EXCEPT
SELECT "Photo ID" FROM Comment WHERE "User ID" NOT IN (1, 3)
)
lets do three joins, one for john, one for sara, one for everyone else. Then we'll limit what we get back with the where clause.
select p.*
from photo p
left join comment john on john.photo_id=p.photo_id and john.user_id=1
left join comment sara on sara.photo_id=p.photo_id and sara.user_id=3
left join comment everyone_else on everyone_else.photo_id=p.photo_id and everyone_else.user_id<>3 and everyone_else.user_id<>1
where
everyone_else.id is null
and john.id is not null
and sara.id is not null
There are a couple of ways to do this. One is to use count with case:
select photoid
from comment
group by photoid
having count(distinct userid) = 2
and count(case when userid not in (1,3) then 1 end) = 0
SQL Fiddle Demo
Basically, make sure 2 users have commented and then make sure only user 1 or 3 commented.
You could use an intersection to find only the common photos, which would exclude photos commented by John but not Sarah, or vice versa
select photo_id from comment where user_id = 1
intersect
select photo_id from comment where user_id = 3

SELECT certain fields based on certain WHERE conditions

I am writing an advanced MySQL query that searches a database and retrieves user information. What I am wondering is can I select certain fields if WHERE condition 1 is met and select other fields if WHERE condition 2 is met?
Database: users
------------------------
| user_id | first_name |
------------------------
| 1 | John |
------------------------
| 2 | Chris |
------------------------
| 3 | Sam |
------------------------
| 4 | Megan |
------------------------
Database: friendship
--------------------------------------
| user_id_one | user_id_two | status |
--------------------------------------
| 2 | 4 | 0 |
--------------------------------------
| 4 | 1 | 1 |
--------------------------------------
Status 0 = Unconfirmed
Status 1 = Confirmed
OK, as you can see John & Megan are confirmed friends while Chris & Megan are friends but the relationship is unconfirmed.
The query I am trying to write is as follow: Megan(4) searches for new friends I want all of the users except for the ones she is a confirmed friend with to be returned. So, the results should return 2,3. But since a relationship with user_id 2 exists but is not confirmed, I want to also return the status since an entry in the friendship table does exist between the two. If a user exist but there is no connection in the relationship table it still returns that users information but returns status as a NULL or doesn't return status at all since it doesn't exist in that table.
I hope this makes since. Ask questions if you need to.
Why not use a left join or an if-not-exists?
SELECT users.*
FROM (users LEFT JOIN friendships
ON status=1 AND (user_id_one=user_id OR user_id_two=user_id) )
WHERE
status IS NULL
or
SELECT users.*
FROM users
WHERE
NOT EXISTS (SELECT *
FROM friendships
WHERE status=1
AND (user_id_one=user_id
OR user_id_two=user_id))
You can create to separate queries and then UNION the result tables. In each query, add a field that always has the same value.
So something like this should work:
(SELECT id, 'Not Friends' As Status FROM t1 WHERE condition1)
UNION
(SELECT id, 'Unconfirmed' As Status FROM t1 WHERE condition2)
Just make sure the same number and name of fields exists in both queries.

How should joins used in mysql?

If i have two tables like
user table-"u"
userid | name
1 | lenova
2 | acer
3 | hp
pass table-"p"
userid | password
1 | len123
2 | acer123
3 | hp123
as for as i learnt from tutorials I can join these 2 tables using many joins available in
mysql as said here
If i have a table like
role table-"r"
roleid | rname
1 | admin
2 | user
3 | dataanalyst
token table-"t"
tokenid| tname
1 | xxxx
2 | yyyy
3 | zzzz
tole_token_association table-"a"
roleid | tokenid
1 | 1
1 | 2
3 | 1
3 | 3
3 | 1
I have to make a join such that I have to display a table which corresponds
like this "rolename" has all these tokens.How to make this? I am confused. Is it possible to make a join? I am liking mysql a lot. I wish to play with queries such that not playing. I want to get well versed. Any Suggestions Please?
It's easiest to see when the column names that need to be joined are named identically:
SELECT r.rname,
t.tname
FROM ROLE r
JOIN ROLE_TOKEN_ASSOCIATION rta ON rta.roleid = r.roleid
JOIN TOKEN t ON t.tokenid = rta.tokenid
This will return only the roles with tokens associated. If you have a role that doesn't have a token associated, you need to use an OUTER join, like this:
SELECT r.rname,
t.tname
FROM ROLE r
LEFT JOIN ROLE_TOKEN_ASSOCIATION rta ON rta.roleid = r.roleid
JOIN TOKEN t ON t.tokenid = rta.tokenid
This link might help -- it's a visual representation of JOINs.

One table, need multiple values from different rows/tuples

I have tables like:
'profile_values'
userID | fid | value
-------+---------+-------
1 | 3 | joe#gmail.com
1 | 45 | 203-234-2345
3 | 3 | jane#gmail.com
1 | 45 | 123-456-7890
And:
'users'
userID | name
-------+-------
1 | joe
2 | jane
3 | jake
I want to join them and have one row with two of the values like:
'profile_values'
userID | name | email | phone
-------+-------+----------------+--------------
1 | joe | joe#gmail.com | 203-234-2345
2 | jane | jane#gmail.com | 123-456-7890
I have solved it but it feels clumsy and I want to know if there is a better way to do it. Meaning solutions that are either more readable or faster(optimized) or simply best-practice.
Current solution: multiple tables selected, many conditional statements:
SELECT u.userID AS memberid,
u.name AS first_name,
pv1.value AS fname,
pv2.value as lname
FROM users AS u,
profile_values AS pv1,
profile_values AS pv2,
WHERE u.userID = pv1.userID
AND pv1.fid = 3
AND u.userID = pv2.userID
AND pv2.fid = 45;
Thanks for the help!
It's a typical pivot query:
SELECT u.userid,
u.name,
MAX(CASE WHEN pv.fid = 3 THEN pv.value ELSE NULL END) AS email,
MAX(CASE WHEN pv.fid = 45 THEN pv.value ELSE NULL END) AS phone,
FROM USERS u
JOIN PROFILE_VALUES pv ON pv.userid = u.userid
GROUP BY u.userid, u.name
Add "LEFT" before the "JOIN" if you want to see users who don't have any entries in the PROFILE_VALUES table.
I didn't say this, which I should have (#OMG Ponies) but I wanted to use this within a MySQL view. And for views to be editable you're not allowed to use aggregate functions and other constraints. So what I had to do whas use the same SQL query as I had described in the initial question.
Hope this helps someone, adding tag for creating views.