What's wrong with this MySQL query (uses LEFT OUTER JOIN)? - php

I'm trying to make a query that returns the following:
All users such that:
-They are not an admin or owner account
-They have the same client_id as the project's client_id
-They are not already in the project_users table with entry project_users.project_id = 9
Here is my MySQL query:
SELECT `users`.`id` as id, `users`.`first_name` as first_name, `users`.`last_name` as last_name, `users`.`username` as username
FROM (`users`)
JOIN `projects` ON `projects`.`client_id` = `users`.`client_id` AND projects.id = 9
LEFT OUTER JOIN `project_users` ON `users`.`id` = `project_users`.`user_id`
WHERE `users`.`user_type` != 'Admin'
AND `users`.`user_type` != 'Owner'
For some reason, this query seems to return all non-super(not owner or admin) users with the same client_id as the project, but does NOT exclude those already in the project_users table (ie. the LEFT OUTER JOIN statement isn't working).
Can anyone tell me what is wrong with the query?
Thanks!

You need to add a filter to find the rows that don't match. Also, your query can be helped by using table aliases:
SELECT u.`id` as id, u.`first_name` as first_name, u.`last_name` as last_name, u.`username` as username
FROM `users` u JOIN
`projects` p
ON p.`client_id` = u.`client_id` AND p.id = 9 LEFT OUTER JOIN
`project_users` pu
ON u.`id` = pu.`user_id`
WHERE u.`user_type` not in ('Admin', 'Owner') and
pu.user_id is NULL;

Related

Specify columns in INNER JOINed table

comment table and post table both has column named user_id
I cannot specify both table's user_id
for using some if else condition later I need both the user_id as a different name (I'm trying to use AS).
I tried different way but query not working:
$sql="SELECT `post_id`, `comment_id`, `comment`, `user_id`, `username`,
`is_marked` `post`.`user_id` AS `p_uid` FROM `comment` INNER JOIN `user` ON
`comment`.`user_id` = `user`.`id` INNER JOIN `post` ON
`user`.`id`=`post`.`user_id` ORDER BY `comment_id` DESC";
$result = mysqli_query($con, $sql);
if ($result) {
while ($row=mysqli_fetch_assoc($result)) {
$post_user_id = $row['p_uid'];
You could alias table name with other name and get column. View example:
SELECT C.comment_id, U.user_id
FROM comment C INNER JOIN user u ON C.user_id = U.id
I would do:
SELECT c.post_id,
c.comment_id,
c.comment,
c.user_id AS c_uid,
c.username,
c.is_marked,
p.user_id AS p_uid
FROM comment c
INNER JOIN user u ON c.user_id = u.id
INNER JOIN post p ON c.user_id = p.id
ORDER BY c.comment_id DESC
Define aliases for the tables, and the selected fields. It makes it simpler to read than putting the table names all the time.
In your PHP you can then reference $row['c_uid'] or $row['p_uid'].

MySQL If Else Statment

Here is an example query:
SELECT tags.serial_number, tags.expiry_date, tags.activation_date, users.email, users.offers, users.newsletter, pets.name as petname
FROM `tags`
JOIN `users` on users.id = tags.user_id
JOIN `pets` on pets.id = tags.pet_id
WHERE users.username = 'myusername' AND users.email = 'itsme#mydomain.com'
In this scenario, the only results that will be returned are those tags that are assigned to both a user and a pet. But what I want is to be able to receive all tags, regardless of whether or not they are assigned to a pet.
If there some kind of "IF there is a pet, show the petname, ELSE just return the pet name as NULL" or something like that?
Try using a Left Outer Join Which will populate rows that don't join to the table with Nulls
SELECT tags.serial_number, tags.expiry_date, tags.activation_date, users.email, users.offers, users.newsletter, pets.name as petname
FROM `tags`
JOIN `users` on users.id = tags.user_id
Left Outer JOIN `pets` on pets.id = tags.pet_id
WHERE users.username = 'myusername' AND users.email = 'itsme#mydomain.com'
The quick answer is to use LEFT JOIN in place of JOIN.
(I'm sure this question has asked on StackOverflow before ...)

SQL select query

I have these tables currently:
assignments(assignment_id, module_id, year, semester, title, number, weighting)
module(module_id, code, name, crn, course_title)
usermodule(usermodule_id, user_id, module_id)
users(user_id, title, first_name, last_name)
I was wondering if anyone could give me guidance on how to get the lecturer name, based on the module_id?
So far I've come up with:
SELECT `first_name` FROM `users` WHERE `assignments.module_id` = '$module_id';
I think I may have to do some form of JOIN query...
try this query
SELECT u.first_name FROM users u
LEFT JOIN usermodule um ON um.user_id = u.user_id
WHERE um.module_id = '$module_id';
How about
SELECT `first_name`
FROM `users` u INNER JOIN
`usermodule` um ON u.user_id = um.user_id
WHERE `um.module_id` = '$module_id';
Try rhis:
SELECT `first_name` FROM
`users` INNER JOIN `usermodule` ON `users.user_id`=`usermodule.user_id`
INNER JOIN `assignments` ON `usermodule.module_id` =`assignments.module_id`
WHERE `assignments.module_id` = '$module_id';
Just try below sql
SELECT `first_name` FROM `users` WHERE user_id = (select user_id from usermodule where module_id = '$module_id' limit 1))
I see poor structuring of your database.
You do not necessarily need a table usermodule to create relationships between the other tables.
If there is a relationship between a user and a module.
make use of a foreign key.
You could have a user_id in the module to link a user to a given module, then have 'module_id' in assignment to link a given assignment to a module, that way in the end, the user, module and assignment are related.
It will then be easy to use JOIN or simple select query

mysql request from two tables when one is empty

I have two tables. And my request return me zero rows if my second table is empty and first - isn't... How can I solve this problem?
Table: users
id username name email
1 myuname myname myemail#domain.com
Table: accounts
customerid phone params
1 +1111 NULL
My sql request is below:
SELECT
A.phone,
A.params,
U.email,
U.username,
U.name
FROM `account` A, `users` U
WHERE A.customerid = U.id LIMIT 1';
The request above return zero rows if my account table is empty and users table isn't...
How can I solve this problem?
Thanks.
You can use a LEFT JOIN:
SELECT
A.phone,
A.params,
U.email,
U.username,
U.name
FROM
`account` A LEFT JOIN `users` U ON A.customerid = U.id
LIMIT 1
A LEFT JOIN will select all rows from the first table and only the rows on the second table that matches. If there is no match, U.email, U.username and U.name will be NULL.
You need to LEFT JOIN your tables.
'account' a LEFT JOIN 'users' u ON u.id = a.customerid
Try this
SELECT
A.phone,
A.params,
U.email,
U.username,
U.name
FROM account a
LEFT JOIN users as u ON u.id = a.customerid;

PHP/MYSQL Query if not in second table

I have two tables. I want to draw a sample of the first table except where the person in the first table is also in a second table. Am having trouble doing this seemingly simple query.
table users
id|name
table catuser
id|userid|catid
I have tried
SELECT u.*,c.userid FROM `users` u
LEFT JOIN `catuser` c
ON (u.id = c.userid AND c.userid <> '197')
WHERE u.id = '1'
and variations to no avail. Would appreciate any suggestions.
How abt. this:
SELECT u.*,c.userid
FROM `users` u
LEFT JOIN `catuser` c
ON u.id = c.userid
WHERE u.id = '1'
AND c.userid <> '197'
AND c.userid is null
SELECT * FROM users WHERE id NOT IN (SELECT DISTINCT userid FROM catuser)
If you want to query only users that have one or more categories, you can use a WHERE EXISTS query:
SELECT u.* FROM `users` u
WHERE EXISTS (SELECT * FROM catuser WHERE catuser.userid = u.id)
Another possibility is to do a left join, and check whether the join succeeded on checking on null:
SELECT u.*, c.* FROM `users` u
LEFT JOIN catuser c ON u.id = c.userid
WHERE c.id IS NOT NULL
If there is no corresponding row in catuser, all catuser fields will be null. By checking whether c.id is not null, you only include the rows with a category.
Note that the join may return a user multiple time, if he is in multiple categories.

Categories