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 = ...
Related
i have two tables like this:
TABLE: users
id | username
----------------
1 | nick1
2 | nick2
TABLE: messages
id | from | to | text
--------------------------------
1 | 1 | 2 | Hi man!
2 | 2 | 1 | Oh, hi.
So, i need SELECT code for replacing "from" and "to" with usernames.
I need SELECT code for my app withe replaced ID´s with usernames :).
Many thanks to all.
You need to join the table messages two times with the table users:
SELECT
m.id,
u1.username,
u2.username,
m.text
FROM
messages AS m
INNER JOIN
users AS u1 ON u1.id = m.from
INNER JOIN
users AS u2 ON u2.id = m.to
You have to join users table twice with aliasses:
select * from messages
join users ufrom on from = ufrom.id
join users uto on uto.id=to
I have three tables:
users
user_id username
---------------------
1 | mrzander
2 | foo
3 | bar
---------------------
interests
interest_id interest
------------------------
1 | cars
2 | power tools
3 | shaving
4 | phones
5 | computers
------------------------
user_interests
id uid iid
-----------------
1 | 1 | 2
2 | 1 | 4
3 | 2 | 3
4 | 1 | 5
-----------------
Basically, I have a table of users, a table of interests, and a table that shows what users have what interests. If I know what user id I want the interests from, what query would give me all of a particular users interests?
In this example, what query would return a table called "Interests" that tells me user_id = 1 likes power tools, phones, and computers?
If you want the result on same row you should use join and group concat
select c.username, group_concat( b.interst)
from user_interest as a
left join interest as b on a.iid = b.interest_id
left join users as c. on c.user_id = a.uid
where c.user_id = 1
group by c.username
or if you need result on different rows se join only
select c.username, b.interst
from user_interest as a
left join interest as b on a.iid = b.interest_id
left join users as c. on c.user_id = a.uid
where c.user_id = 1
Simply join the two tables.
select i.*
from interests i
join user_interests u
on u.iid = i.interest_id
where i.uid = 1;
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;
I have 2 tables:
Users:
------------------------------
id | name
--------------------------
1 | John
2 | Dane
3 | Foo
4 | Bar
Matches Table:
----------------------------
id | userid1 | userid2
----------------------------
1 | 1 | 3
2 | 2 | 4
Question:
From the matches table with id 1, i want to fetch John and Foo in one query. How can i do that ?
I already have one solution but its dull. That is selecting records from matchs tables and then while looping, trigger queries for getting names. .
Just use a JOIN...
SELECT u1.*, u2.*
FROM Matches m
JOIN Users u1 ON u1.id = m.userid1
JOIN Users u2 ON u2.id = m.userid2
WHERE m.id = [ YOUR DESIRED USER ID (for example: 1) ]
SELECT name from Matches, Users where Matches.id = 1 AND (Users.id = Matches.userid1 OR Users.id = Matches.userid2)
This should work.
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