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';
Related
I have to tables: user and userimages.
Right now, I'm using the following query:
SELECT * FROM users u INNER JOIN userimages ui ON u.id=ui.userid;
Yes. It retrieved the records if the userid and id is equal. What I really want to do is to select all records from table users even if its id is not present on the second table (userimages)
My question is how to select all records in the users table with or withou userid on the second table?
You're looking for a LEFT JOIN, which will pull all rows from the first table and join where applicable to the right.
SELECT * FROM users u LEFT JOIN userimages ui ON u.id=ui.userid;
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.
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.
I have two tables and they are as follows:
USERS
ORDERS
I want select all users who have at least 1 order or more in the ORDERS table. I know there is an inline query for this in MySQL, but right now I have to select all users and then make another query seeing if each user has an order - all this using a PHP loop.
What I am doing now is not ethically correct, so I basically just want to select all users who have been referenced in the ORDERS table in ONE MySQL query.
This is a query you should be using
select distinct u.* from users u
inner join orders o on o.user_id = u.id;
Note the distinct and u.*. This query will not select fields from orders and it will not select the same user twice (if one has more than one order).
Demo: http://sqlfiddle.com/#!2/6ebcc/3
You can use mysql join syntax. Assuming both of your tables has userid column, this is the example :
SELECT * FROM USERS a JOIN ORDERS b ON
a.UserId = b.UserId
This is a simple database operation, see here for the explanation join
Before delving into the issue, first I will explain the situation. I have two tables such as the following:
USERS TABLE
user_id
username
firstName
lastName
GROUPS TABLE
user_id
group_id
I want to retrieve all users who's first name is LIKE '%foo%' and who is a part of a group with group_id = 'givengid'
So, the query would like something like this:
SELECT user_id FROM users WHERE firstName LIKE '%foo'"
I can make a user defined sql function such as ismember(user_id, group_id) that will return 1 if the user is a part of the group and 0 if they are not and this to the WHERE clause in the aforementioned select statement. However, this means that for every user who's first name matches the criteria, another query has to be run through thousands of other records to find a potential match for a group entry.
The users and groups table will each have several hundred thousand records. Is it more conventional to use the user defined function approach or run a query using the UNION statement? If the UNION approach is best, what would the query with the union statement look like?
Of course, I will run benchmarks but I just want to get some perspective on the possible range of solutions for this situation and what is generally most effective/efficient.
You should use a JOIN to get users matching your two criteria.
SELECT
user_id
FROM
users
INNER JOIN
groups
ON groups.user_id = users.users_id
AND groups.group_id = given_id
WHERE
firstName LIKE '%foo'
You don't need to use either a UNION or a user-defined function here; instead, you can use a JOIN (which lets you join one table to another one based on a set of equivalent columns):
SELECT u.user_id
FROM users AS u
JOIN groups AS g
ON g.user_id = u.user_id
WHERE g.group_id = 'givengid'
AND u.firstName LIKE '%foo'
What this query does is join rows in the groups table to rows in the users table when the user_id is the same (so if you were to use SELECT *, you would end up with a long row containing the user data and the group data for that user). If multiple groups rows exist for the user, multiple rows will be retrieved before being filtered by the WHERE clause.
Use a join:
SELECT DISTINCT user_id
FROM users
INNER JOIN groups ON groups.user_id = users.user_id
WHERE users.firstName LIKE '%foo'
AND groups.group_id = '23'
The DISTINCT makes sure you don't have duplicate user IDs in the result.