I have 2 tables : Users & User_friends, the users table holds data of the user like name, password, id, etc. the user_friends table holds both of the id's of user and another user from the same users table:
id | user_id | friend_id
1 | 1 /Ben | 2 / Dave
2 | 1 /Ben | 3 / Rob
So as you can see - Ben is friend with both Dave and Rob ( the names are just for visual representation).
I want to create a query that get all the users - but adds an indicator for
who is friend with the logged user - which be passed as a paramater from PHP later on (bear with me here please).
The result i'm looking for is something like this:
id | user | isFriend
2 | john | true
3 | bob | false
4 | sam | false
Is it possiable with MYSQL or should i do it on the client side, with JavaScript?
Here are the tables.
Thanks
This query should give you the results you want. For each user in the users table, it gives you id, name and whether they are friends with (in this example) user 4 (this being the value you would pass as a parameter to the query):
SELECT u.id, u.name,
EXISTS(SELECT *
FROM user_friends
WHERE user_id = u.id AND friend_id = 4) AS isFriend
FROM users u
Results (for your sample data)
id name isFriend
1 roy 0
2 amit 1
3 oren 1
4 yaniv 0
5 shai 0
11 adi 0
12 eran 1
13 Nir 0
14 Ron 0
Updated SQLFiddle
Related
I can't really explain it in words because it is hard for me. I'll just show you what I need to accomplish.
So lets say I have 2 tables, admin and records.
admin table with sample data:
a_id | a_name
1 | haime
2 | joseph
Record table with sample data:
r_id | r_amount | r_a_create_by | r_a_update_by
1 | 99 | 1 | 2
So I have a transaction record that is created by admin with ID of 1 and updated by ID of 2. Now how can I make a select query of that? If I want the output of something like :
1 | 99 | haime | joseph
You need to join the admin 2 times. This should work:
select r.r_id, r.r_amount, a.a_name, b.a_name
from records r
join admin a on r.r_a_create_by = a.a_id
join admin b on r.r_a_update_by = b.a_id
I have 3 tables i want to join all tables each other. But my 3rd table not working.
See my table -
users
id | username |is_active
----------|----------------|------------
1 | chinu | 1
2 | sradhanjali | 1
3 | User3 | 0
settings
id | user_id | public_msg_notification
----------|-----------|---------------------------
1 | 1 | 1
2 | 2 | 1
3 | 3 | 1
friends
id | user_id | friend_id | is_block
----------|-----------|---------------------------
1 | 3 | 1 | 0
2 | 1 | 2 | 1
3 | 3 | 2 | 0
Query
SELECT a.username FROM users a
JOIN settings b
JOIN friends c ON(a.id=c.user_id OR a.id=c.friend_id)
WHERE a.username IN('john','piter','rahul','sradhanjali')
AND a.id != '1' AND a.is_active=1
AND a.id=b.user_id AND b.public_msg_notification=1
AND c.is_block=0 GROUP BY a.username
I have run this query in my local only sradhanjali username fetched. But this user is_block=1 in the friends table.
I think My third table friends not working. I want to show that result those usernmes where is_block=0. In above data my output should be zero(0) But I am getting 1 record while execute above query.
We had a chat discussion and I think this question is not meant to be on SO for the most part. I did promise if I could figure it out I would try to provide some insight. At this point I think this is a correct approach, but it is very specific to this instance.
SELECT u.username FROM users u
JOIN (SELECT
IF(u.id=f.user_id, f.friend_id, f.user_id) as ids
FROM users u
JOIN friends f ON (f.user_id=u.id OR f.friend_id=u.id)
WHERE
u.id=$SOME_ID AND f.is_block=0) friends ON (u.id=friends.ids)
JOIN settings s ON (s.user_id=friends.ids)
WHERE s.public_msg_notification=1 AND u.is_active=1
GROUP BY friends.ids
By trying to be too specific you aren't able to open up the query any more and have to do a nested query inside. This should get all users you are friends with THEN see which users are accepting public notifications and are active. I'm fearing this will fail. But this at the least will put you in the right direction.
I have these 2 tables: users and friendship. i would like find the friends in common between two user, i tried to do some query with alias but doesn't show my results.
Table users
user_id | name | surname
1 Luca Jhon
2 Paul Red
3 Jin Blue
4 Diana Lars
Table friendship
id_friendship | id_user_sender | id_user_receive | confirm
1 1 2 2
2 2 3 2
3 1 3 2
4 1 5 2
Should be show this one if i am the user called Luca (id 1 ) and search the realtion with Paul (id 2)
name | surname | id_user |
Jin Blue 3
Any idea? Thank you
Friendship is, presumably, reciprocal. Your friendship table only has one-way relationships.
So, the idea is to create all possible friendships in both directions. Then to aggregate by the first and test the second for each of the users you are interested in:
select u.*
from (select id_user_sender as id1, id_user_receive as id2
from frienship f
union all
select id_user_receive as id1, id_user_send as id2
from frienship f
) f join
users u
on f.id1 = u.user_id
group by id1
having max(id2 = 1) > 0 and
max(id2 = 2) > 0;
I want to create a limit of 6 rows with this logic:
Select user_id's that are in the friends list (and also in the same table)
if there are less that 6, select then another RANDOM users,but not in friends list (until limit of 6)
if there are not 6 user_id's, add some "dummy" users id (with id 0)
all "real" users must be distinct (with id > 0)
id | friends_list | name
1 2,3,5 John
2 1,7,9 Michael
3 1,2,5 Tom
4 3,2,6 Larry
The expected result must be something like this (for a given user e.g. id=1):
2, 3, 4, 0, 0, 0
You should probably store your data differently and use joins to select the data out of multiple tables. There are also other ways you might think about storing data also depending on your use cases.
PEOPLE TABLE
id | name
1 | John
2 | Michael
3 | Tom
4 | Larry
PEOPLE_FRIENDS TABLE
id | person_id | friend_id
1 | 1 | 2 //In this case John is friends with Michael
2 | 3 | 1 //In this case Tom is friends with John.
The following select would pull id's for John's friends.
SELECT * FROM PEOPLE `P` INNER JOIN PEOPLE_FRIENDS `PF` ON P.id = P.person_id WHERE P.id = 1
Again a million different ways to go about writing that query as well, but this will get you pointed in the right direction I think.
Ok here's my problem. Assume a customer has access to a number of regions defined in a CustomerRegions table:
CustomerRegionID | CustomerID | RegionID
----------------------------------------
1 | 1 | 1
2 | 1 | 2
Assume that customer 1 has three users 1, 2, and 3. For each user we can specify to which of the CustomerRegions they have access via a table UserRegions:
UserRegionID | UserID | CustomerRegionID
----------------------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 2
So user 1 will have access to both Customerregions and user 2 will only have access to CustomerRegion 2.
If there are UserRegions specified for a given user then only those CustomerRegions are present in the result set, but if no UserRegions are specified for a given user then all CustomerRegions are present in the result. I want to get all accessible regions per user of a given customer. The result I am looking for is something like this:
CustomerID | UserID | RegionID
------------------------------
1 | 1 | 1
1 | 1 | 2
1 | 2 | 2
1 | 3 | 1
1 | 3 | 2
My question is can this be done in a single query and how?
Edit:
I seem to have it working now:
SELECT CustomerID,
UserID,
RegionID
FROM users
LEFT JOIN customerregions ON customerregions.CustomerID = users.CustomerID
LEFT JOIN userregions ON userregions.UserID = users.UserID AND userregions.CustomerRegionID = customerregions.CustomerRegionID
LEFT JOIN regions ON regions.RegionID = customerregions.RegionID
WHERE (userregions.UserID IS NOT NULL
OR (SELECT COUNT(1) FROM userregions WHERE userregions.UserID = users.UserID) = 0)
AND CustomerID = 1
The extra count query in the where seems to do the trick. Thanks #Pablo Martinez for your help. However if someone knows of a better way to do this please let me know.
I'm aggre with #diEcho, the table structure is very confusing
have you try to do a join?
Select CustomerID, UserID, RegionID
from UserRegions join CustomerRegion
on CustomerRegion.CustomerRegionID=UserRegions.CustomerRegionID
where customerID=1