PHP/MYSQL Query if not in second table - php

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.

Related

how write sql query with multiple join?

I have fours tables and I wanted to join all three tables with the one table.
I have listed my problem as follows:
Tables:
users
user_training
user_courses
user_certificates
I wanted to get the data from [2,3,4] tables that user_id field matches with the users table ID field.
When I try the INNER JOIN it gives me the result for users that are common in all the tables, But I just wanted to check the [2,3,4] tables with the table [1] Records.
My Query...
SELECT A.training_name AS 'training_name', C.course_name AS 'course_name', D.certificate_name AS 'certificate_name'
FROM user_training AS A INNER JOIN users AS B ON A.user_id=B.ID INNER JOIN user_courses AS C ON B.ID = C.user_id INNER JOIN user_certificates AS D ON B.ID = D.user_id;
Thanks in Advance.
use left join
select u.* from users u
left join user_training ut on ut.user_id=u.user_id
left join user_courses uc on uc.user_id=u.user_id
left join user_certificates uct on uct.user_id=u.user_id
With this one you are getting all users and their respective trainings:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
Changing *_trainig to *_courses or *_certificates will return all users with respected courses or certificates.
If you need to get data in one query, try this one:
SELECT *
FROM `users`
LEFT JOIN `user_training` ON `users`.`id` = `user_training`.`user_id`
LEFT JOIN `user_courses` ON `users`.`id` = `user_courses`.`user_id`
LEFT JOIN `user_certificates` ON `users`.`id` = `user_certificates`.`user_id`
If user has no trainings, courses, certificates all remaining fields will be null-ed.

join and null values rows not showing

I have this statement:
SELECT personen.*, klassen.naam as klas FROM `personen`
JOIN `klassen` ON `klassen`.id = `personen`.Id
WHERE `rol` = "Docent" ORDER BY id
I know that it basically puts the column klassen.naam into personen by checking if the foreign key K_Id is the same as the id of Klassen:
In the table above I have 4 results, these are all the people who have a K_Id assigned, however there are 2 more people who have a K_Id of null:
My problem is that when K_Id is null it doesn't return anything at all, which makes sense, I just don't know how to fix it.
My question is: How can i write the statement so that it still shows all the other rows where the K_Id value is null?
SELECT personen.*, klassen.naam as klas
FROM `personen`
JOIN `klassen`
ON `klassen`.id = `personen`.Id
WHERE `rol` = "Docent"
ORDER BY id
You should use left join if you want all of your data from personen Table where personen.id is not present in klassen table it look like this
SELECT personen.*, klassen.naam as klas
FROM `personen`
LEFT JOIN `klassen`
ON `klassen`.id = `personen`.Id
WHERE `rol` = "Docent"
ORDER BY id
You would use a left join for this purpose. Assuming rol is in the personen table:
SELECT p.*, k.naam as klas
FROM `personen` p LEFT JOIN
`klassen` i
ON i.id = p.Id
WHERE p.`rol` = 'Docent'
ORDER BY p.id;
If rol is in the klassen table, then:
SELECT p.*, k.naam as klas
FROM `personen` p LEFT JOIN
`klassen` i
ON i.id = p.Id AND i.`rol` = 'Docent'
ORDER BY p.id;
Note that I also introduced table aliases in the query. These make queries easier to write and to read.
Also, this will return all rows in the personen table (subject to the where), including non-matches. If you just specifically wanted NULL values, then you would use:
SELECT p.*, k.naam as klas
FROM `personen` p JOIN
`klassen` i
ON i.id = p.Id or p.id is null
WHERE p.`rol` = 'Docent'
ORDER BY p.id;
I'm guessing you really want the left join version.

Trying to JOIN an empty table nothing returns

I have a problem trying to JOIN an empty table (comments table) to my existing prepared statement.
This is working perfectly:
// prepare images
if ($stmt = $mysqli->prepare(" SELECT uu.*, m.*,
(
SELECT COUNT(*)
FROM img_likes AS t
WHERE t.img_id = uu.imgID AND t.user_id = ?
) AS user_likes,
(
SELECT COUNT(*)
FROM img_likes AS t
WHERE t.img_id = uu.imgID
) AS total_likes
FROM user_uploads AS uu
INNER JOIN members AS m ON m.id = uu.user_id
ORDER BY up_time DESC")) {
$stmt->bind_param('i', $user_id);
$stmt->execute(); // get imgs
// foreach print images
// working as expected
}
And I don't know why if I JOIN another table (img_comments) that is empty, the images are not printed... if I add a row to the table and refresh the page, one image is printed...
The statement that I'm trying and it's not working is this:
SELECT uu.*, m.*, ic.*,
(
SELECT COUNT(*)
FROM img_likes AS t
WHERE t.img_id = uu.imgID AND t.user_id = ?
) AS user_likes,
(
SELECT COUNT(*)
FROM img_likes AS t
WHERE t.img_id = uu.imgID
) AS total_likes
FROM user_uploads AS uu
INNER JOIN members AS m ON m.id = uu.user_id
INNER JOIN img_comments AS ic ON ic.img_id = uu.imgID
ORDER BY up_time DESC
Why is only printing images based on the number of the table rows?? I also tried LEFT JOIN but I'm not too familiareize with this. I only use INNER JOIN in other scripts and I never had a problem like this.
I would appreciate any optimization to my query.
What does an inner join do? It joins all records of table a with all matching records of table b. So when there are no records in table b, there is no match for any record of table a, hence no result at all. Why does this surprise you?
A left join is an outer join (short for LEFT OUTER JOIN). It means: Give me all records of table a with all matching records of table b, and when there is no match then give me the record of table a anyhow. This seems to be what you are wanting here. But you say you tried it. I don't see how this would fail in your query.
A typical error for an outer join not to work would be to have some field of b in your where clause (e.g. where b.id > 100). As the outer-joined records have no matching b record, all b fields are null, so that such a where clause would fail. You'd just get matches again, just like with the inner join.
EDIT: As to optimization, you can get the two counts in one pass by counting conditionally:
SELECT
uu.*, m.*, ic.*,
il.count_user AS user_likes,
il.count_total AS total_likes
FROM user_uploads AS uu
INNER JOIN members AS m ON m.id = uu.user_id
LEFT OUTER JOIN img_comments AS ic ON ic.img_id = uu.imgID
LEFT OUTER JOIN
(
select
img_id,
count(*) as count_total,
count(case when t.user_id = ? then 1 end) as count_user
from img_likes
group by img_id
) AS il ON il.img_id = uu.imgID
ORDER BY uu.up_time DESC;
As far as I know, INNER JOIN will only retrieve data which have both data. So if let say the table that you join have no data with that join condition. It will not return any data at all.
LEFT JOIN is just a normal join. It will retrieve data on both table. But if the joined table is empty, then only the primary table will have data, the secondary table will have null as its data.
You can just modify your code, replacing INNER JOIN with LEFT JOIN and see if it works/

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;

MySQL: Select rows where `project_id` equals any `project` with specific `user_id`

This can't be too hard, but I don't know what the term is I'm looking for. I'm sure you guys can help me out. :)
I have a table tasks with rows that have a column project_id. Each project_id refers to (the id of) a row in the projects table. Each project belongs to a certain user which is why it has a column user_id.
I now want to select all tasks from this table where the project_id equals any project of a certain user.
Or put more simply:
Each TASK belongs to a PROJECT which belongs to a USER.
I want to create a SELECT-statement to receive all TASKS that belong to a specific USER. The only link between each TASK and a USER is through the PROJECT.
How do I accomplish this?
Thank you very much for your help! :)
JOIN the tables:
SELECT
t.*
FROM tasks t
INNER JOIN projects p ON t.project_id = p.project_id
INNER JOIN users u ON p.user_id = u.user_id
WHERE u.user_id = #AcertianUserId
SELECT
tasks.*
FROM
users
INNER JOIN
projects ON users.id = projects.user_id
INNER JOIN
tasks ON projects.id = tasks.project_id
WHERE
users.id = 1
SQL Fiddle
select t.*
from tasks t
join projects p on p.id = t.project_id
join users u on u.id = p.user_id
where u.id = 17
Try :
SELECT t.*
FROM user u, projet p, task t
WHERE u.id = p.user_id
AND p.id = t.project_id
AND u.id = your_id
Or (same result) :
SELECT t.*
FROM user u
INNER JOIN projet p ON u.id = p.user_id
INNER JOIN task t ON p.id = t.project_id
WHERE u.id = your_id

Categories