I got table Followers which looks like this:
| id | follower_id | user_id |
| 1 | user1 | user2 |
| 2 | user2 | user3 |
| 3 | user2 | user1 |
| 4 | user4 | user3 |
| 5 | user3 | user2 |
I need to count instances when two users are following each other, in this case result should be 2, because user1 and user2, user2 and user3 is following each other.
I tried all sorts of combinations of LEFT JOIN, SELECT COUNT() FROM table WHERE field IN(), but I think I'm missing something... I feel though that I'm close to the goal with this query
SELECT
u.id,
u.name,
u.img,
ifnull((follower_id and user_id),0) as `match`
FROM table_users u
LEFT JOIN (select user_id from table_followers where follower_id = 14) followers on u.id = followers.user_id
LEFT JOIN (select follower_id from table_followers where user_id = 14) following on u.id = following.follower_id
WHERE u.id = 14 and (follower_id or user_id)
With this query I'm trying to figure out how many followers user_id_14 has.
Is it possible to achieve this with pure MySQL, or I should go around this with PHP loops?
This is making a headache to me for three hours and I can't find solution.
SELECT
CONVERT(COUNT(*)/2, UNSIGNED) as counter
FROM followers u
INNER JOIN (
select
user_id,
follower_id
from followers
) as f
on u.user_id = f.follower_id
WHERE u.follower_id = f.user_id
This may help you..
Select count(*)
From followers f1
joins followers f2 ON f1.follower_id = f2.user_id
and f1.user_id= f2.followerid;
Related
Suppose I have a table named users consist of columns: user_id, user_name, user_created_by.
+------------------+----------------------+-------------------+
| user_id + user_name + user_created_by +
+------------------+----------------------+-------------------+
| 1 | John | 1 |
| 2 | Ann | 1 |
| 3 | Paul | 2 |
| 4 | King | 2 |
| 5 | Dirk | 3 |
+------------------+----------------------+-------------------+
The value of user_created_by is the user_id who created that record. Now, I want to make a query that results one specific row with added column let's say user_created_by_name which is the user_name of the user_id from the user_created_by. Suppose we want to get "Paul"'s record with who (the name) create it (temporary new column). For ease of understanding this is my expected result:
+----------+--------------+-------------------+------------------------+
| user_id | user_name | user_created_by | user_created_by_name |
+----------+--------------+-------------------+------------------------+
| 3 | Paul | 2 | Ann |
+----------+--------------+-------------------+------------------------+
this is my query using codeigniter:
$query=$this->db->query("SELECT *,
(SELECT user_name FROM users WHERE user_id = user_created_by)
AS "user_created_by_name" FROM users WHERE user_id=3);
But my result are:
+----------+--------------+-------------------+------------------------+
| user_id | user_name | user_created_by | user_created_by_name |
+----------+--------------+-------------------+------------------------+
| 3 | Paul | 2 | NULL |
+----------+--------------+-------------------+------------------------+
You culd use a self join (join the same table two time) using alias for fere to the tables as different sets of data
SELECT a.user_id, a.user_name, a.user_created_by, b.user_name as user_created_by_name
from users a
inner join user b on a.user_created_by = b.user_id
where a.user_id = 3
use self join
select u1.user_id, u1.name as user_name,
u2.user_created_by
,u2.user_name as createdby from users u1
join users u2 on u1.user_id=u2.user_created_by
where u1.user_id=3
You can solve this problem using a JOIN.
$sql = "SELECT users.user_id, users.user_name, user_created_by_name.user_name,
FROM users JOIN users AS user_created_by_name ON users.user_id = user_created_by_name.user_id WHERE users.user_id = 3";
$query=$this->db->query($sql);
If you you have users that were not created by another user use a LEFT JOIN instead:
$sql = "SELECT users.user_id, users.user_name, user_created_by_name.user_name,
FROM users LEFT JOIN users AS user_created_by_name ON users.user_id = users.user_id WHERE user_created_by_name.user_id = 3";
$query=$this->db->query($sql);
This will work:
SELECT a.user_id as User_id,
a.user_name as Name,
b.user_id as Created_by_user_id,
b.user_name as Created_by_name
FROM users AS a
INNER JOIN users AS b
ON a.user_id = b.user_created_by
WHERE a.user_id = 3
It is called a self-join, which is used when combining two records of the same table.
Tables:
users friends
+-------+----+ +-----+-----+
| name | id | | id1 | id2 |
+-------+----+ +-----+-----+
| user1 | 1 | | 1 | 2 |
+-------+----+ +-----+-----+
| user2 + 2 |
+-------+----+
In my database id1 in the friends table is the dominant column, which means that if id1 = 1 and id2 = 2 then user1 is friends with user2 but not the other way around.
I'm trying to select all users from the users table that don't have X as id1 in friends. This is because I don't want userX to be able to find friends that he's already added.
Here is my failed attempt:
SELECT * FROM users LEFT JOIN friends ON users.id != friends.id2 WHERE friends.id1 = X AND users.id != X;
I added users.id != X since we don't want to return the user himself in a search for other users.
You can use an outer join to do that, you attempt went into the right direction:
select u.*
from users u
left join friends f
on u.id = f.id2
where f.id1 is null
An outer join returns NULL for every non-matched item at least.
using not in() (more efficient than not exists() in mysql)
select *
from users
where id != xi
and id not in (
select id.2
from friends
where friends.id1 = x
)
using not exists()
select *
from users
where id != x
and not exists (
select 1
from friends
where friends.id1 = x
and friends.id2 = users.id
)
users table:
id | win | lose
0 | 1 | 2
1 | 2 | 8
games table:
id | user1 | user2 | data
0 | 0 | 1 | 'some text'
The DB is for a simple multiplayer game. I need a query to get user2, data, and win-lose data for both users. Is this possible in MySQL at all? Any help would be greatly appreciated.
You have to JOIN the table users two times like this:
SELECT
u1.win AS win1,
u1.lose AS lose1,
u2.win AS win2,
u2.lose AS lose2,
...
FROM games g
INNER JOIN users u1 ON g.user1 = u1.id
INNER JOIN users u2 ON g.user2 = u2.id
You can join the users table twice:
SELECT
games.user2 AS user2_id
games.data AS games_data
user1.win AS user1_win,
user1.lose AS user1_lose,
user2.win AS user2_win,
user2.lose AS user2_lose
FROM games
JOIN users AS user1 ON user1.id = games.user1
JOIN users AS user2 ON user2.id = games.user2
WHERE games.id = ...
I have an implementation messages system.
My problem is, I would like to know whether a user already has a thread with another user and if so what is the mid
I have a messages_recips table which look like this
---------------------------
| mid | seq | uid | status|
|--------------------------
| 4 | 1 | 1 | A |
| 4 | 1 | 2 | A |
---------------------------
if user id 1 having a thread with user id 2 I hold 2 rows with same mid.
I know I can create 2 sqls to achieve what I'm asking for, but I'm trying to do it in 1 sql.
As noted by Waqar Janjua, the key to this is a self-join query:
SELECT m1.mid
FROM messages_recips AS m1
JOIN messages_recips AS m2 ON m1.mid = m2.mid
WHERE m1.uid = 1
AND m2.uid = 2
I think you have to write a self-join query:
Select u.uid, u1.uid from tablename u
INNER JOIN tablename u1 on u.mid = u1.mid
You will get all the users who have the same mid.
In order to get only user1 and user2 records you have to place a where clause at the end of the query lik this.
Select u.uid, u1.uid from tablename u
INNER JOIN tablename u1 on u.mid = u1.mid
Where ( u.uid In ( 1,2 ) OR u1.uid In ( 1,2 ) ) ;
I'm very new to joins, so far I've been doing relatively simple joins but this one got me stumped.
I have 2 tables that look like this:
Friends Table
friendship_id | friend_init | friend_accept | status
1 | 18 | 10 | 1
2 | 13 | 18 | 0
Users Table
user_id | email | username | password | kittens, etc...
10 | -- | -- | -- | --
13 | -- | -- | -- | --
18 | -- | -- | -- | --
If I am trying to select all the info about user_id 18's friends from both tables where the friends record status = 1, what would the join look like?
SELECT users.*
FROM users JOIN friends ON friends.friend_accept = users.user_id
WHERE friend.friend_init = 18 AND friend.status = 1
UNION
SELECT users.*
FROM users JOIN friends ON friends.friend_init = users.user_id
WHERE friend.friend_accept = 18 AND friend.status = 1
Drop the UNION... if your database is symmetrical (you have both (18,10) and 10,18), or if you only want to see friendships from one side.
Select *
from users
LEFT join Friends
ON Users.User_ID = Friends.Friend_init
OR users.User_ID = Friends.Friend_Accept
Where Friends.Status = 1
LEFT join is used because if a user has no FRIENDS yet; the user will still show up.
select *
from user u
join friends f
on u.user_id = f.friends_accept
where u.user_id = 18 and f.status = 1
union
select *
from friend f
join user u
on u.user_id = f.friend_status
where u.user_id = 18 and f.status = 1