Conditional mysql join? - mysql

I have two tables, one table is for users, and the other table is user_interested_in_who table.
The idea is if a user is interested in another user, I will insert the ids for those two users to the user_interested_in_who table
The schema for my tables are:
Users user_interested_in_who
id id
name this_user (id from the users table)
interested_in_this_user (id from the users table)
interested_or_not (1 = yes, 0 = no)
So I want to query my tables by joining them together, my query is like this:
SELECT users.id, users.name, user_interested_in_who.interested_or_not
FROM users
LEFT JOIN user_interested_in_who
ON user_interested_in_who.this_user = 1 *//1 is the id of the current log in user*
GROUP BY users.id
The problem with this query is that the interested_or_not column all have 1. Even when the interested_or_not column have 0 in the record
My question is how can I query it to return NULL if no record is found on the user_interested_in_who table and how can I query it to return 0 in the user_interested_or_not column if the record is 0
Edit:
How can I make it return this kind of table:
table:
id | name | interested_or_not
1 jess NULL
2 erika 1
3 jil 0
4 adrian NULL
....
1050 mich 1

You need to use a LEFT OUTER JOIN but with no literal value in your ON clause.
This will return all the entries in the first table and either their match in the second table or NULL.
SELECT users.id, users.name, user_interested_in_who.interested_or_not
FROM users
LEFT OUTER JOIN user_interested_in_who
ON user_interested_in_who.this_user = users.id
There is no need to GROUP, and you don't need a WHERE for your example case (which you have showing all the values?). If you do want to limit to a User ID then modify as follows:
SELECT users.id, users.name, user_interested_in_who.interested_or_not
FROM users
LEFT OUTER JOIN user_interested_in_who
ON user_interested_in_who.this_user = users.id
WHERE users.id = 1

You need to JOIN not LEFT JOIN. JOIN will return only the records in both tables You also need a where clause instead of using the user's logged in id to create the join.
You dont need to group anything, that's why you're always getting 1.
This should do what you want.
SELECT users.id, users.name, user_interested_in_who.interested_or_not
FROM users
JOIN user_interested_in_who
ON user_interested_in_who.this_user = users.id
WHERE users.id=1;

Related

SQL - Joining 2 tables, show all from tbl1 and data from tbl2 depending on condition

Im trying to show a full list of all users in my DB aswell as extra information from a second table depending on whether they have a record in the second table
Im using MySQL, ive tried a few left join/right join union combos but nothing i have works
SELECT users.id, users.name, success.URL_ID, success.docreqid FROM users
LEFT JOIN success ON users.id = success.userid
where docreqid IS NULL
union
SELECT users.id, users.name, success.URL_ID, success.docreqid FROM users
RIGHT JOIN success ON users.id = success.userid
where docreqid = 1;
I have a small table of 10 users. Only one user in my db has a record in the success tbl against docreqid '1'.
I want a table of ALL users and the URL_ID for their form if they have submitted it.
The above code works perfectly for this.
If i change the last line to:
where docreqid = 2;
I only get 9 results (the user with a record for docreqid '1' is missing).
I would like this table to show all 10 users and 'NULL' in the URL_ID & docreqid columns until they have completed the required action.
A left join should do what you want:
SELECT u.id, u.name, s.URL_ID, s.docreqid
FROM users u LEFT JOIN
success s
ON u.id = s.userid AND s.docreqid = 1;
The LEFT JOIN returns all rows in the first table -- no WHERE is filtering results. The ON only matches rows in success that meet the additional condition in the ON clause.

Query Check Status from other table

I have following query
select * from user_profile
Now i want to add where condition check user status from other table (users)
select * from user_profile,users where users.status!=0
Please Do Not Recommend Join i following old join query
Thanks
If you don't want to use join, you have to use a subquery. I guess both tables have a column like userId:
select * from user_profile where userId in (select userId from users where status != 0)
try this using sub query
select * from user_profile where status!=(select status from users where id =? or status=?)
what you compare id or status in subquery
Assuming you have a relation column (say user_id or perhaps profile_id or something -- not sure until you share sample data as requested.) between the two tables, you can join the two table and filter the rows like this:
select *
from user_profile p
join users u on p.user_id = u.user_id
where u.status != 0;
If you want every column from both tables in your query, you can use:
SELECT * from user_profile, users WHERE user_profile.user_id = users.id AND users.status! = 0
Where I assumed you have some column(like user_id - user_profile
and id - users in my example) that links both tables together.

MySQL query from 2 tables confusion

I have a query that I would like to run but is not returning the expected results.
So my tables are like this
users (has two columns)
user_id,name
users_archive (has the same two columns)
user_id,name
I want to basically run a query that lists user_id from the respective table where the username matches what I'm searching for
For my example I have a user called MikeBOSS in users_archive with an user_id of 123 (there is no MikeBOSS in users table)
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users_archive.name='MikeBOSS'
WHERE users.name='MikeBOSS';
but that returns no results
SELECT users.user_id, users_archive.user_id
FROM users, users_archive
WHERE (users.name='MikeBOSS' OR users_archive.name='MikeBOSS');
That returns a bunch of results from the users table that are incorrect.
Could someone maybe point me in the correct direction?
You do not want a JOIN, you want a UNION. Look
SELECT users.user_id, 'users'
FROM users
WHERE users.name='MikeBOSS'
UNION
SELECT users_archive.user_id, 'archive'
FROM users_archive
WHERE users_archive.name='MikeBOSS';
A join condition normally links two tables. Yours does not:
ON users_archive.name='MikeBOSS'
A join condition that does link the two tables might look something like:
ON users.name = users_archive.name
If you wonder about the number of rows this returns, check each table individually. Is there even a row with name = 'MikeBoss' in the users_archive table?
Change
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users_archive.name='MikeBOSS'
WHERE users.name='MikeBOSS';
To
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users.name = users_archive.name WHERE users.name='MikeBOSS';
May be this could work if you have no relation between 2 tables. Using left join gives you record from one table even if its not present in other.
SELECT users.user_id, users_archive.user_id
FROM users
LEFT JOIN users_archive ON users.userid = users_archive.userid and users.name = users_archive.name
WHERE users.name='MikeBOSS';

I have three tables in a many to many relationship want to get results as raw data boolean

Say I have
users.id
roles.id
usersroles.users_id, usersroles.roles_id
How can I get the following headers for say user 1 with a triple join (I think IF clause in select?):
users.id, roles.id, (tinyint)has_the_role
1,1,0
1,2,1
1,3,1
1,4,0
You will need two joins for that:
SELECT users.id, roles.id,
CASE WHEN usersroles.roles_id IS NULL THEN 0 ELSE 1 END AS has_the_role
FROM users
INNER JOIN roles
LEFT JOIN usersroles ON users.id = usersroles.users_id AND roles.id = usersroles.roles_id
First you join the users table with the roles table (no condition, so all possibilities are joined) and after that you check if there is a usersroles entry for a specific combination.

Combining RIGHT JOIN with COUNT

I'm trying to get a list of the number of entries in the changes_cc table by each user. Not all users have made entries into it, however for some reason it's returning "1" for each user that has 0 entries. I'm assuming that it's because it's counting the entries in the JOINed table. How can I make it so that it is "0" instead?
SELECT COUNT(*) as num, users.id, realname, username
FROM changes_cc
RIGHT JOIN users
ON changes_cc.user_id = users.id
GROUP BY users.id
I think this should work -- count a specific field in the changes_cc table vs counting *:
SELECT u.id, realname, username, COUNT(c.id) as num
FROM users u
LEFT JOIN changes_cc c
ON u.user_id = c.id
GROUP BY u.id
I prefer reading a LEFT JOIN over a RIGHT JOIN, but they are both OUTER JOINs and work the same.
You should not be using COUNT(*) (counts the record including null values) because it will normally give atleast 1 since it returns all records from the right table. If you specify the column name to be counted, it will gove you the result you want because COUNT only counts for NON_NULL value.
SELECT COUNT(changes_cc.user_id) as num,
users.id,
realname,
username
FROM changes_cc
RIGHT JOIN users
ON changes_cc.user_id = users.id
GROUP BY users.id
Instead of using count(*), use count(changes_cc.user_id).
The problem is that you are counting rows (with the *) rather than counting the non-NULL values in the "right-joined" table.