MySQL SELECT from two tables, replace ID's with usernames - mysql

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

Related

MySQL query from data normalized tables

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;

Join Tables On Two Columns

i've been trying to join to one table but using two different columns. Ok so i've got the following tables:
Users:
--------------------------------------------------
| user_id | name |
--------------------------------------------------
| 1 | Bob |
--------------------------------------------------
| 2 | John |
--------------------------------------------------
Table2:
--------------------------------------------------
| user_one | user_two | field3 |
--------------------------------------------------
| 1 | 2 | something here |
--------------------------------------------------
Is their any way to join the user's table using both fields. Something like this:
SELECT
table2.*,
users.name
FROM table2
INNER JOIN users
ON users.user_id = table2.user_one AND users.user_id = table2.user_two
I've tried the above query but it doesn't return anything.
Edit
I would want to return something like this:
--------------------------------------------------
| user_one_name | user_two_name | field3 |
--------------------------------------------------
| Bob | John | something here|
--------------------------------------------------
You need two joins. This is often done using left join in case one or both user ids do not match:
SELECT t2.*, u1.name as user1name, u2.name as user2name
FROM table2 t2 LEFT JOIN
users u1
ON u1.user_id = t2.user_one LEFT JOIN
users u2
ON u2.user_id = t2.user_two;
It sounds like you want to get the name of both users in a single row. Assuming so, then you can join to the users table multiple times:
SELECT
table2.*,
u1.name user1name,
u2.name user2name
FROM table2 t2
INNER JOIN users u1 ON u1.user_id = t2.user_one
INNER JOIN users u2 ON u2.user_id = t2.user_two
As mentioned in a comment, if it's possible you have user ids in table2 that do not exist in the users table (or you allow null values to be stored), you can use an outer join instead to get your desired results.
However, if that is the case, you may have larger issues with your database design. You shouldn't have foreign keys that don't have corresponding primary keys unless those are perhaps nullable.

Mysql select query - stuck in where condition

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.

MySQL: Get two rows as one

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 = ...

joining 4 tables

I am new to this kind of relational type of database design. I just designed the database in this manner. However, I am quite confused on this JOIN of MySQL. What should be my query to join all this table. If you can see the table users is the reference of all the tables.
users
+----------+----------------+-----------------+
| users_id | users_level_id | users_status_id |
+----------+----------------+-----------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
+----------+----------------+-----------------+
users_credentials
+----------+---------------------------+-----------------------------+----------------------------+
| users_id | users_credential_username | users_credential_email | users_credential_password |
+----------+---------------------------+-----------------------------+----------------------------+
| 1 | super | super#gmail.com | $5$e94e9e$vptscyHjm8rdX0j6 |
| 2 | admin | admin#gmail.com | $5$fVuOmySyC0PttbiMn8in0k7 |
+----------+---------------------------+-----------------------------+----------------------------+
users_level
+----------------+-------------------------+
| users_level_id | users_level_description |
+----------------+-------------------------+
| 1 | Super Administrator |
| 2 | Administrator |
+----------------+-------------------------+
users_status
+-----------------+--------------------------+
| users_status_id | users_status_description |
+-----------------+--------------------------+
| 0 | Disabled |
| 1 | Enabled |
+-----------------+--------------------------+
Try this
SELECT u.*, uc.*, ul.*, us.*
FROM users u
INNER JOIN users_credentials uc
ON u.users_id = uc.users_id
INNER JOIN users_level ul
ON u.users_level_id = ul.users_level_id
INNER JOIN users_status us
ON u.users_status_id = us.users_status_id
Note the use INNER JOIN: this means that if a user does not have corresponing record on joined table it won't be shown; if you need to return every user even without matching record on related tables, change INNER JOIN with LEFT JOIN.
EDITED after user comment:
If you want to return just some column, define it as this example
SELECT uc.users_credential_username AS username,
uc.users_credential_email AS email,
uc.users_credential_password AS pwd,
ul.users_level_description AS level,
us.users_status_description AS status
This is a simple query that will join all of them
select *
from users
left join users_credentials
on users_credentials.users_id = users.users_id
left join users_level
on users_level.users_level_id = users.users_level_id
left join users_status
on users_status.users_status_id = users.users_status_id
EDIT
if you want to fetch data from different tables
user this
select users.* , users_credentials.* , users_level.* , users_status.*
from users
left join users_credentials
on users_credentials.users_id = users.users_id
left join users_level
on users_level.users_level_id = users.users_level_id
left join users_status
on users_status.users_status_id = users.users_status_id
I think this look like this :
SELECT * FROM users
LEFT JOIN user_credentials ON users.user_id = user_credential.user_id
LEFT JOIN user_level ON users.users_level_id = users_level.users_level_id
and so on..
Use this type of query....
SELECT c.*, l.*, s.*
FROM users AS u
INNER JOIN users_credentials AS c ON (u.users_id = C.users_id)
INNER JOIN users_level AS l ON (u.users_level_id= l.users_level_id)
INNER JOIN users_status AS s ON (u.users_status_id= s.users_status_id)
Where you can specify the field what you want in .* ...
Join is used to fetch data from normalized tables which have foreign key relation with the reference table.
For the above table with join you can fetch data among two tables with the help of reference table.
For example
Select * from users a JOIN users_credentials b
ON a.user_id=b.user_id JOIN users_level c
ON c.users_level_id=a.users_level_id
where users_credential_username='super';
The result of this query will give you the detail like users_level_description for the user with users_credential_username=super.