How to join two columns to the same table [duplicate] - mysql

This question already has answers here:
Sql join, 2 tables, same fields
(3 answers)
Closed 8 years ago.
I have an SQL table called messages, it has three columns
1. UserFrom uniqueidentifier
2. UserTo uniqueidentifier
3. Messagen varchar(50)
This table is used to store messages sent from one user to another, it stores the UserId from the aspnet_Users instead of the username, now I need to create a view that shows the UserFrom and UserTo as names by getting the Usename from the aspnet_Users table using the UserId in the table messages.
Thanks in advance

You need to join aspnet_Users table twice with different alias names:
SELECT U1.Username as UserFrom,U2.Username as UserTo, M.Message
FROM Messages M JOIN
aspnet_Users U1 ON U1.UserId=M.UserFrom JOIN
aspnet_Users U2 ON U2.UserId=M.UserTo
Explanation:
Here aspnet_Users table it joined twice with different alias names U1,U2. And each username is fetched from the respective table.

Just join with the user table twice.
SELECT
t2.name AS userFrom,
t3.name AS userTo,
t1.Message
FROM messages t1
LEFT JOIN aspnet_Users t2 ON t1.UserFrom = t2.UserId
LEFT JOIN aspnet_Users t3 ON t1.UserTo = t3.UserId

You need to join twice to the table with the user data - once for each user involved:
select uf.username from_username, ut.username to_username, message
from messages m
join aspnet_Users uf on uf.id = userfrom
join aspnet_Users ut on ut.id = userto
This sql will work in both databases you have tagged the question with (or any other).

Related

How to select columns from different tables in 1 query when I don't have a foreign key [duplicate]

This question already has answers here:
How do you join on the same table, twice, in mysql?
(3 answers)
Closed last year.
I have the following tables in my database:
As you can see my matches table contains 2 columns named: team_id_1 and team_id_2, what I want is to select all column from the matches table and also get the team_name_en and team_name_ar from the teams table for team_id_1 and team_id_2.
Note that my matches table has 1 foreign key: group_id
You can join all tables , teams twice, to get all colums.
SELECT
m.*, g.*, t1.*, t2.*
FROM
matches m
INNER JOIN
`groups` g ON g.group_id = m.group_id
INNER JOIN
teams t1 ON t1.team_id = m.team_id_1
INNER JOIN
teams t2 ON t2.team_id = m.team_id_2
But you should only join tables, if you need the columns.
also you should only get the columns that are needed. but when you run the query you at least can see how it works

How do I Select from two tables using Two WHERE Conditions?

I have an application that has a referral system where users can use their user_id to refer another user. All users are on the same table. The referrals table stores the user_id for the referrer and for the referred.
Users Table:
Referrals Table:
I want to query the names of both users (referrer and referred) from the user's table using their user_id that is populated in the referrals table.
Expected Result:
select t2.name as referrer_name,t3.name as referred_name
from REFERRALS_TABLE t1
left join USER_TABLE t2 ON t1.referrer=t2.user_id
left join USER_TABLE t3 ON t1.referred=t3.user_id
I method is to join the User_table twice -
SELECT U1.name referrers_name, U2.name referred_name
FROM Referrals R
JOIN Users U1 ON R.referrer = U1.user_id
JOIN Users U2 ON R.referrd = U1.user_id
You use sql UNION if get two table data and multiple where clouse.
SELECT * FROM oauth_clients WHERE user_id=2
UNION
SELECT * FROM photos where gallery_id=12

Select multiple row even first table is null mysql

I've encountered a problem on my wordpress website. I have multiple select mysql query from one page then decided to merge because it's in from same table. But the problem is when first selected table is null, all other row is affected and becomes null. And if the first select table is has a value, then the others selected rows will be display.
Here's my sample mysql query code:
SELECT u1.meta_value as name, u2.meta_value as birthday,
u3.meta_value as place
FROM wp_usermeta u1
LEFT JOIN wp_usermeta u2 ON u1.user_id = u2.user_id
LEFT JOIN wp_usermeta u3 ON u1.user_id = u3.user_id
WHERE u1.user_id = 1092
AND u1.meta_key = 'name' AND u2.meta_key = 'birthday' AND u3.meta_key = 'place'
you can use union and 2nd query as a right join below is sample code
The LEFT JOIN keyword returns all records from the left table
(table1), and the matched records from the right table (table2). The
result is NULL from the right side, if there is no match.
The UNION operator is used to combine the result-set of two or more
SELECT statements.
Each SELECT statement within UNION must have the same number of
columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
select t1.* from t1 left join t2 on t1.id=t2.id
union
select t2.* from t1 right join t2 on t1.id=t2.id

mysql - display the values that exists in ALL the tables

i did a quick search here but wasn't able to find the answer i was looking for. here is what i would like to do:
i am creating a simple event registration tracking system.
i have a database called registration and in that database i have created 10 tables called table1, table2..... table10. all these tables just has one column: user_id
i have one more table called userinfo and this table has 2 columns called user_id and username .
i need to create a query that checks whether ANY user_id exists in ALL the tables. if it does, it will display that user's name from the userinfo table.
i was thinking of using count function to get the total count for each user_id and if i get a count of 11, it will tell me that user_id exists in every table.
but I am not sure if that the proper way to go. any ideas will be appreciated. i am not good with queries so if someone could post an example as well. thanks a lot in advance.
You can simply do an inner join on all the tables on the user_id field, and display the username from userinfo table only if a result exists in the joined query :
SELECT username FROM userinfo it WHERE EXISTS (
SELECT user_id FROM table1 t1
INNER JOIN Table2 USING(user_id)
INNER JOIN Table3 USING(user_id)
INNER JOIN Table4 USING(user_id)
INNER JOIN Table5 USING(user_id)
INNER JOIN Table6 USING(user_id)
INNER JOIN Table7 USING(user_id)
INNER JOIN Table8 USING(user_id)
INNER JOIN Table9 USING(user_id)
INNER JOIN Table10 USING(user_id)
WHERE t1.user_id = it.user_id
);

Mysql alias and left join

I have 2 two tables.One table contains 2 user id (like fast user and second user) in a row. Another table contains user data (like user name).
How can I retrieve 2 user name using left join?? I would like to have sql query. How can I get that result in php ??
Thanks
Foysal
Try this one,
SELECT a.fastuser, b.*,
a.secondUser, c.*
FROM table1 a
LEFT JOIN table2 b
on a.fastUser = b.userID
LEFT JOIN table2 c
on a.secondUser = c.userID
You can join the same table twice and use different alias names for tables and user_name columns:
select u1.user_name as first_username,
u2.user_name as second_username
from some_table t
left join user_table u1 on t.first_user_id = u1.user_id
left join user_table u2 on t.second_user_id = u2.user_id
Say Table1 contains user info :--id,id_c,email,age etc.
Table 2 contains user_name :--id,user_name
here id in table1 refers to id in table 1(for 1st user)
here id_c in table1 refers to id in table 2(for 2nd user)
Then query would be like
select a.user_name,b.user_name,u.email from Table1 as u
left join Table2 as a on u.id=a.id
left join Table2 as b on u.id_c=b.id
You can go through my blog-post for reference purpose
http://codebucket.co.in/for-mysql-savvy-multiple-joins-with-alias/