I have 2 tables 'Users' and 'Friends':
Table: Users(username,email,location,online_status)
Table: Friends(Friend_rq_from,Friend_request_to,status)
'Users' table is for number of registered users in a website
and 'Friends' table for individual users for maintaing thier friends..
i want to count eg: mithun's friends from 'Friends' table(1 indicating they are friends in Friends table) who are online in Users table..(1 indicate online in 'users' table)
its a little bit difficult for me because, 'Friends' table have 2 fields which 'mithun' can be in 'Friend_rq_from' field or 'Friend_request_to' field...
how to find this?
NB: Friend_rq_from: Indicating who start a friend request,
Friend_request_to: Indicating to whom a request sent(or who is waiting to accept)
SELECT COUNT(distinct u.username)
FROM Users u
INNER JOIN Friends f
ON (u.username = f.Friend_rq_from AND f.Friend_rq_to='mithun' AND f.status=1)
OR (u.username = f.Friend_rq_to AND f.Friend_rq_from='mithun' AND f.status=1)
WHERE u.online_status = 1 AND u.username <> 'mithun'
Related
i am creating a restaurant review website. in my review table i have a foreign key called user_id and idk how to use it to display the username which is in the user table
my user table
my review table
so my question is how do i display the username from this? what mysql statement do i have to write. I am lost on what to do
Assuming you want to try and get the review text along with the user name from the corresponding user you can use a join to combine the info for example:
SELECT u.username, r.review_text
FROM reviews r
LEFT JOIN users u
ON (u.user_id = r.user_id)
I assumed the users table is called users and reviews table is called reviews but update those as necessary each is "aliased" as u and r respectively and then tables are joined
If the relationship between the two tables is mapped out correctly you should be able to run a query to fetch the name of each user. Try to avoid any N+1 query though
I have 2 tables in my MySQL database for users and groups. I need to relate users with groups and groups with the users. The only way that came my mind is having a group_ids col for users and user_ids col for groups. I have to do like this because I will show users' groups that they registered in their profile and I will show registered users in groups' users pages.
In this option I need to to store group ids for users like "2,5,14" and same in groups for registered user ids like "22,24,15 ...".
It sounds okey to me but parsing IDs on back-end from commas is not sounds "professional". And also I have concerns for the performance when there is huge amounts of users in a group.
I know this seems like a opinion based question but I have a question and I think it is not opinion based.
Is there a usage like this in "data science"? I mean, is this a common usage or am I missing something here because I really can't think something else.
You could create a new table called user_group wich stores the user_id and group_id as foreign key and primary key
The you can get all groups by user with
SELECT item1, item2...
FROM user
INNER JOIN user_group on user.user_id = user_group.user_id
INNER JOIN group on user_group.group_id = group.group_id
WHERE user.user_id = id;
I have 2 table in my DB, first is users, and second is friends.
In the users tables I have a column named `friend' this columns have a random ID of each one, and this ID you used for calling someone to subscribe with your URL.
but if an old person want to become your friend I add other table named friends
content the random ID + ID_USER
now, I want to SELECT * FROM users, friends WHERE users.friends = 'BlaBla' OR friends.RANDOMID = 'BlaBla'
I try with this code, but I doesn't work, it's give all persons having the random
in calling friend
SELECT *
FROM users, friends
WHERE users.Role = "User"
AND users.call = :random
OR (users.ID = friends.ID_USER
AND friends.random = :random)
Any solution?
My web app needs to ALWAYS query 2nd degree connections. Each user has say 200 friends & those friends have 200 friends each. I could use some help in determining the right database (and table structure) to make this web app fast & responsive.
Business logic: Users search their 1st & 2nd degree connections to get a list of other users who use a specific service (stored in one column as unsigned int). That's the only functionality of this app.
Table structure:
User Table: User_ID (pk), Facebook_ID (sk), Name, Specific-service, Location
Relationship Table: still undecided.
Question: I read many posts & searched the web for "social networking database design". However, these applications feel much different than mine. I will have many users (+10 mil) but a small database & run only one query as described in business logic.
Additional info: Users can register (& subsequently log-in) only using their Facebook account. Their friends will be invited (via Facebook) to also register. The Relationship Table will be populated once friends register (only active/not-blocked/not-pending friends). Thus I can get rid of "friendship status" column from Relationship Table.
You need a table that has two ids in it; it will define a 'Friend'. Is this relationship symmetric? That is, if A is a friend of B, is B a friend of A? Well, I will assume there are 2 rows when both occur.
Then
CREATE TABLE Friends (
user1 ...,
user2 ...,
PRIMARY KEY(user1, user2),
INDEX( user2, user1)
) ENGINE=InnoDB;
SELECT a.name, c.name
FROM Users AS a
JOIN Friends AS ab ON ab.user1 = a.user_id
JOIN Users AS b ON b.user_id = ab.user2
JOIN Friends AS bc ON bc.user1 = b.user_id
JOIN Users AS c ON c.user_id = bc.user2
WHERE a.user_id = ?
I have a table called users, that looks like this for example:
Table: users
username id
Simon 6
Ida 7
And a relationships table
Table: Relationships
me partner
6 7
Now for every relationship that is created the me and partner columns will get the id depending on which one of the users sent the request.
If Ida sends a request to Simon, the column looks like this: me: 7 partner: 6
because Ida is id number 7 and Simon is number 6.
But if Simon sends a request then the columns looks like this: me: 6 partner: 7
What i want to do is to write a query that will get the right id from the relationships table for every user with a relationship, and then use the id to Join the users table and get the partners username and print it out.
My problem is that the columns Me and Partner can have different values depending on which of the 2 users sended the request first.
How do i write a query that prints out the right information for each and every user and give them the right id of their partner?
How the output should look like:
From simons point of view*
: You are in a relationship with Ida.
From Idas point of view *
: You are in a relationship with Simon.
I want to offer an alternative.
Join the user table to the relationship table either on the me column or the partner column.
Then join the user table back onto the relationship table using the same criteria.
SELECT u.username,
FROM users u
JOIN relationships r ON u.id = r.me OR u.id = r.partner
JOIN users u2 ON r.me = u2.id OR r.partner = u2.id
WHERE u.id = 'THE USER YOU CARE ABOUT'
Untested. If you throw up some CREATE TABLEs and INSERT INTOs we can all have a play.
If you want to get all users which have a relationship with some user (ex.: id:2) you specify, I would use something like this:
SELECT id, username
FROM user,
(SELECT receiver_id as rel_id FROM relationship WHERE sender_id=2 union
SELECT sender_id as rel_id FROM relationship WHERE receiver_id=2) tab
WHERE id=rel_id
The inner statement means you are selecting all the relationships in which 2 was the sender and all the relationships in which 2 was the receiver and stacking them in the same column (union).
After that, the table user is used to get the user name from the id.
Select username as username,
(select username name from users uSubq where uSubq.id = r.partner) as partnername
from users u join relationships r on u.id=r.me
UNION
Select username as username,
(select username name from users uSubq where uSubq.id = r.me) as partnername
from users u join relationships r on u.id=r.partner;
Here is the test for the query: Sql Fiddle
If either Ida sent request to Simon or the reverse but not both, use UNION ALL for better performance.