How to select from two tables with IF? - mysql

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?

Related

SQL Friends List Query

I am new to this database SQL language so I will try to make it as simple as possible. (I am using MySQL Workbench)
I have a table for User and I want multiple users to be friends with each other. For that I created the friends table where it has a user_id and friend_user_id. So let's say we have (1,2), (2,3). I want this to be read as: "2 is friends with 1 and 3, 1 is friends with 2, 3 is friends with 2". So when inserting on this friends table I never do something like this (1,2),(2,1). I'm looking for a procedure that by receiving an user_id as parameter to return all his friends whether they are in the user_id column or the friend_user_id column. For example, if I look for user 2's friends it should appear 1 column with 1 and 3, because 1 and 3 are friends with 2.
To be more specific, when I call get_friends(2) it should appear
[1]
[3]
Even though these are in different columns on the friends table.
You can use IN to check if either column is equal to the ID of the user you want to look up and a CASE ... END to take the column which is not equal to the ID of the user you want to look up.
SELECT CASE
WHEN user_id = 2
THEN user_friend_id
WHEN user_friend_id = 2
THEN user_id
END friend
FROM friends
WHERE 2 IN (user_id, user_friend_id);
Alternatively you could use a UNION ALL approach, which might perform better as it can use indexes on user_id or user_friend_id.
SELECT user_id friend
FROM friends
WHERE user_friend_id = 2
UNION ALL
SELECT user_friend_id friend
FROM friends
WHERE friend_id = 2;
But this is only better if there are such indexes. If there aren't, it may need two scans on the table opposed to the first approach only needing one. So it's worse in that case.
Use UNION ALL to get all friends, parameterize this query:
select friend_user_id as friend_id from friends f where f.user_id = 2 --parameter
union all
select user_id as friend_id from friends f where f.friend_user_id = 2 --parameter

Randomize a winner pair from the subscriptions

I have a project, where customer needs a winnner pair for events. The users of this site can "like" other user's (just like on FB), they subscribe to a particular post, and the script will generate a winner pair from the subscribers.
So I need a SQL query to randomize a winner pair from the list of pairs, where the users liked each other, and subscribed to a particular post.
How do i do that ?
I cant write a query that, because i got unexpected results.
I have 3 tables : events, likes, subs (and users ofc)
events table : event_id, event_name
subs table: sub_id, event_id, uid
likes table: liker, liked (the two uid from the users table)
Now I can make pairs from the likes table (i self-joined the table where liker = liked AND liked = liker) and randomized, but how can I join the subs and the events tables to the likes table to achieve that a randomized pair will be a subscribed users for a particular event too ?
My current query looks like this :
SELECT L.liked AS T1, L.liker AS T2
FROM likes AS L, likes AS K
WHERE L.liked = K.liker
AND L.liker = K.liked
ORDER BY rand( )
LIMIT 0 , 1
I googled everything about joins for one week, but i cant achieve that.

How do i get relationships between users?

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.

how to find a user available online by checking two tables?

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'

MySQL select where equal to multiple values

I have two databases, where I store who is following who and another which stores the posts the user makes.
I want to select all of the people a user is following from the following database and echo out the usernames of those who that user is following and query the posts database for posts of that user.
My problem is what if a user is following multiple users,
I echoed out of the user id's of the people this user is following and I get
44443344330
When I separate each id with commans, I get:
44,44,33,44,33,0,
so let's give that a variable of $user_ids;
$user_ids = "44,44,33,44,33,0, ";
the query:
$get_posts = mysql_query("SELECT * FROM posts WHERE userid = '$user_ids'");
but all it does is show the records of the first user id, 44.
How can I retrieve all of the records for all the users?
The query should be:
SELECT * FROM posts WHERE userid IN (44,44,33,44,33,0)
However, you may have to rethink your data model and make sure it is normalized, so that you can express this construction directly in the databse without echoing into a comma-separated string.
Why do you have two databases? Do you mean two tables?
Maybe you want to use IN
SELECT * FROM posts WHERE userid IN ($user_ids)
Given that you have two tables, not two databases, the easiest way to match multiple values for a specific column would be the following:
SELECT * FROM posts WHERE userid IN (44,44,33,44,33,0)
*A small point that I ran into when doing this. If you are matching to a column that is of type VARCHAR(n), be sure to wrap your matching values in 'single quotes', not "double quotes"
e.g:
SELECT * FROM posts WHERE name IN ('foo','bar','alpha','beta')
Assuming you have two tabels, not databases, and that the table (lets call it "friends") which describes who is following who is like
table friends(
userid
friendid
)
then query to get posts posted by X's friends would be
SELECT
p.*
FROM posts p
JOIN friends f ON(p.userid = f.friendid)
WHERE f.userid = X
$get_posts = mysql_query("SELECT * FROM posts WHERE userid in '($user_ids)'");
$var= str_replace(","," or userid = ","userid =$your_data_from_db");
your myqsl_query = SELECT * FROM posts where $var