mysql - display the values that exists in ALL the tables - mysql

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
);

Related

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

MySQL join on whichever column is not null

I have a table with a bunch of columns, but we only need to look at two of them. I'm trying to join another table on this table, but all we know about these two columns is that one will be null and the other won't:
client_id | note_id
The main table wants to join client_id (if not null) on clients.id OR note_id on notes.id if clients.id is null.
This will work for you. This is very basic query I wrote. Make changes if required.
SELECT * FROM YOUR_TABLE t
LEFT OUTER JOIN clients c ON t.client_id = c.id
LEFT OUTER JOIN notes n ON t.note_id = n.id
WHERE c.id IS NOT NULL OR n.id IS NOT NULL
Assuming there are 3 tables involved (the main table that contains client_id and note_id columns, clients table, and notes table), you can use a query such as this:
(select *
from mainTable inner join clients on mainTable.client_id = clients.id)
union
(select *
from mainTable inner join notes on mainTable.note_id = notes.id
where mainTable.client_id is NULL);
The above query contains 2 queries where each query will output rows where the joining column is not null. The results are then combined using union.
You can use coalesce in the join on clause. See demo here:
http://sqlfiddle.com/#!9/99911/2. If client id is null then use note id to join table1 and table2.
Select t1.client_id, t1.note_id,t2.client_id, t2.note_id
From table1 t1
Join table2 t2
on coalesce(t1.client_id, t1.note_id) =coalesce(t2.client_id, t2.note_id)

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

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

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/

problems with Join query

Hi I've got data in two tables my first table contains
first name
last name
my second table contain
userid
first name
last name
I'm trying to write a sql query to get the userid of a particular user but I'm getting empty set while executing the query. Could anyone please verify that the query I'm using is right? It seems ok to me
select users.id
FROM TABLE1 AS r
LEFT JOIN TABLE2 AS users
ON (users.firstname = r.firstname
AND users.lastname=r.lastname)
You use twice the same table (TABLE2), but in the description you state that you have two tables.
I am not sure but i think you want this:
select users.id
FROM TABLE1 AS r
INNER JOIN TABLE2 AS users
ON (users.firstname = r.firstname AND users.lastname=r.lastname)
select users.id
FROM TABLE1 AS r
INNER JOIN TABLE2 AS users
ON (lower(ltrim(rtrim(users.firstname))) = lower(ltrim(rtrim(r.firstname))) AND lower(ltrim(rtrim(users.lastname)))=lower(ltrim(rtrim(r.lastname))))