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

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

Related

sql select everything from the table that does not appear in a second table

I have three tables -
Users [Id,Name]
Matches_A [Id1,Id2]
Matches_B[Id1,Id2]
I know the user_id of the current user.
For each Id1, in either matches table, there are many entries. That is there are many matches for each user in both tables.
I need to select all of the users who are not in either matches table.
I tried this query -
SELECT * FROM Users
JOIN Matches_A ON Users.id = Matches_A.id2
JOIN Matches_B on Users.id = Matches_B.id2
WHERE Matches_A.id1 != $userid
AND Matches_B.id1 != $userid;
This doesn't work.
The problem is that there are entries in the Matches table with the same id2 but different id1s. That means that even if I exclude a row where the id1 matches the userid, that user (id2) could still be returned because there is another row with the same user where the id1 does not match the user_id.
If that made no sense, let me rephrase it. It would be easy to select everything that I don't want to be returned.
SELECT * FROM Users
JOIN Matches_A ON Users.id = Matches_A.id2
JOIN Matches_B on Users.id = Matches_B.id2
WHERE Matches_A.id1 != $userid
AND Matches_B.id1 = $userid;
Would return all of the rows that I don't want. How can I write a query that will get me all the other rows.
P.S. It's pretty easy to do this with a sub-query, but I'm worried that will be slow especially with 3 tables.
Think not in or not exists instead of join:
SELECT *
FROM Users u
WHERE u.id NOT IN (SELECT a.id2 FROM Matches_A) AND
u.id NOT IN (SELECT b.id2 FROM Matches_B);
This version assumes that id2 is never NULL in either table.
You can try with NOT EXISTS also
SELECT u.*
FROM Users u
WHERE NOT EXISTS (SELECT a.id2 FROM Matches_A a WHERE a.id2 = u.id
UNION ALL
SELECT b.id2 FROM Matches_B b WHERE b.id2 = u.id)
The simplest solution is to use EXCEPT
WITH Subset as (SELECT id from users EXCEPT
(SELECT id2 from Matches_A
UNION
SELECT id2 from Matches_B))
SELECT * FROM Users NATURAL JOIN Subset;

mysql query result issue with join

I have multiple tables as table_1 has id , p_code, profile_status, name and table_2 has id, p_code, availablity and table_3 has id, p_code, status...
How to get all records form all tables depend on p_code.
table_2 and table_3 has few records. if p_code not in table_2 and table_3 then echo 'no' in results.
currently i am using my query as below
select t.id, t.p_code,t.name,t.num_rooms, t.profile_status, t.distance FROM (
( SELECT id , p_code, profile_status, name,num_rooms, 3956 * 2 * ASIN(SQRT( POWER(SIN(($origLatAirport - latitude)*pi()/180/2),2)
+COS($origLatAirport*pi()/180 )*COS(latitude*pi()/180)
*POWER(SIN(($origLonAirport-longitude)*pi()/180/2),2)))
as distance FROM property WHERE profile_status=1 having distance < ".$dist." ) ) as t
How to add table_2 and table_3 and fetch results.
Pleasr reply soon. I am stuck here.
In your query you are doing CROSS JOIN and what you desire, is probably INNER JOIN.
In MySQL the CROSS JOIN behaves like JOIN and INNER JOIN of without using any condition.
The CROSS JOIN returns all rows form user multiplied by all rows from user_inbox - for every user you get inboxes of all users.
You should specify condition for your JOIN statement.
$sql_alt = mysql_query(
"select i.*,u.images, u.firstname, u.lastname
from user_inbox i INNER JOIN user u ON i.to_id = u.user_id
where i.to_id = '$user_id'");
Also it is good habit have the same names for primary and foreign keys, so I think you should have user_id or user_id_to instead of to_id in your user_inbox table. This is of course not absolutely necessary.

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/

Query optimisation, NOT IN

On the page I have an one query, which is bring page loading speed down.
SELECT * FROM users1 WHERE user_id NOT IN (SELECT user_id FROM users2)
USERS1 is the table with all users of the site, a lot of data.
USERS2 is the table of following eachother users.
So, I need to select some users, which is not alredy following.
Any ways of optimisation?
I'm posting real query:
SELECT users.*
FROM (
SELECT user_id
FROM users
WHERE user_id!=156 AND user_id NOT IN (
SELECT follower_id
FROM following
WHERE user_id=156
) ORDER BY RAND() LIMIT 2
) AS temp
JOIN users ON users.user_id = temp.user_id;
Indexes on user_id fields.
Query took 0.0912 sec
This query works not bad on the testing server with less data, but it takes up to 3 seconds on live one.
LEFT JOIN way:
SELECT *
FROM users1
LEFT JOIN users2 ON users1.user_id = users2.user_id
WHERE users2.user_id IS NULL
NOT EXISTS way:
SELECT *
FROM users1
WHERE NOT EXISTS (SELECT 1 FROM users2 WHERE users1.user_id = users2.user_id)
Working examples: http://sqlfiddle.com/#!2/611f2/1
You should use LEFT JOIN for best performance as:
SELECT a.*
FROM users1 a
LEFT JOIN users2 b
ON a.user_id = b.user_id
WHERE b.user_id IS NULL;
Also have a look at Visual explanation of joins
This should do the trick:
SELECT users1.* FROM users1
LEFT JOIN users2 on users1.user_id=users2.user_id
WHERE users2.user_id IS NULL