mysql select/delete using join over four tables - php

I have four tables (in [] are columns):
users [id]
products [id]
productRatings [id,value,user,product]
comments [id,product,user]
I would like to select/and ultimately delete productRatings where there are no associated comment by the same user for that product. That is, if user has rated product but did not comment, that rating should be deleted.
I believe I could achieve that by using two queries, first:
SELECT user, product FROM productRatings
and then for each row:
SELECT COUNT(*) FROM comments WHERE product=productRatings.product AND user=productRatings.user
and then something like
if $queryAbove==0 : DELETE FROM productRatings WHERE id=productRatings.id
I would like to solve this via JOIN and learn more by example rather than dig through JOIN tutorials.

You only need the productratings and comments table - the following works:
delete pr
from productratings pr
left join comments c
on pr.userid = c.userid
and pr.productid = c.productid
where c.productid is null
And there's a demo here: http://sqlfiddle.com/#!9/89575/1

DELETE FROM `productRatings` WHERE productRatings.id NOT IN (
SELECT productRatings.id FROM `productRatings` JOIN `comments` ON productRatings.user = comments.user AND productRatings.product = comments.product )
I'd make copies of the tables in question and test that the above works on the test tables before using this on your production tables.
Basically, the nested select will return all productRatings id's that the user who wrote the rating also made a comment on. Using NOT IN, it will delete all ratings that the user who made the rating didn't also comment.
As pala_ highlighted in the comments below, because this method uses nested sql, it will perform worse than a method that only uses joins on larger tables.

You should be able to just join the fields in the query and then check if the comment is empty, like so...
DELETE FROM `productRatings`
JOIN `comments` ON (`productRatings`.`product` = `comments`.`product`)
WHERE `comments`.`comment_field` IS NULL
AND `productRatings`.`user_id` = ?;
The IS NULL may need to be replaced with = '' depending on the database engine you use.
Remember to test on a testing instance of the DB first!

DELETE FROM productRatings WHERE id IN (SELECT pr.id FROM productRatings pr
LEFT JOIN comments c on c.product = pr.product
WHERE c.user = pr.user
AND pr.comment IS NULL)

Related

SQL Inner Join query doesn't return all records, but instead the first one ever made

SELECT user.name, comments.cdata, comments.likes FROM comments
WHERE pid = $postNum
INNER JOIN user ON comments.uid = user.uid
ORDER BY cdate
Quick Notes:
I am a beginner, please don't be rude to me, I am trying to learn more
Yes, I have tried LEFT JOIN, but that just returns an SQL sintax error
My database is like this:
2 tables, 1 one is comments, comments has comments.cdata, comments.likes and comments.uid, the user one has the name of the user.
What I have been trying to accomplish is getting the name of the user with the comment data, instead of UID and comment data.
I also can not use 2 queries, due to me getting all the records and then displaying them on page via PHP for each.
Your query is syntactically incorrect. JOIN is an operator in the FROM clause. WHERE is a clause that follows the FROM clause.
In addition, I think the cdata and cdate should be the same thing, although I don't know what.
I also recommend using table aliases. So:
SELECT u.name, c.cdata, c.likes
FROM comments c JOIN
user u
ON c.uid = u.uid
WHERE c.pid = $postNum
ORDER BY c.cdata

Mysql query does not look right

I want to update a field in my table based in another table and I executed this query below but I think it's not right.. it looks like it worked but is it correct? Is there any situation where it might fail?
UPDATE users SET page = (SELECT page_name FROM pages WHERE user_id = id)
My table USERS has a column id and page. My table PAGES has a column page_name and user_id. Is the code above right?
It may fail if pages has more than one page_name per user_id. I find UPDATE a INNER JOIN b ON some_conditions SET a.fieldA = b.fieldB; to be much more readable. It does have the same failure scenario, and can be harder to "fix" for such scenarios; but correlated subqueries (your version) tend to be significantly slower.
Also, style note, UPDATE users AS u SET u.page = (SELECT p.page_name FROM pages AS p WHERE p.user_id = u.id); would've eliminated the need for your last two sentences and (more importantly):
make it so the next developer that has to look at the query does not have to look at the database to find out (or remember) what fields go to what tables.
make it so the query does not break if an id field later gets added to pages.
Instead of subselect In mysql you can use UPDATE JOIN
UPDATE users
INNER JOIN pages on pages.user_id = users.id
SET users.page = pages.page_name
Whenever you have more than one table in a query, you should always use qualified column names -- and preferably aliases. So, your version of the query would be:
UPDATE users u
SET page = (SELECT p.page_name FROM pages p WHERE p.user_id = u.id);
Next, you have to consider whether the subquery might return more than one row. If so, you have to limit it to one row. There are various ways, SELECT MAX(p.page_name), LIMIT 1, and SELECT GROUP_CONCAT(p.page_name) all come to mind.
Next, you are updating all rows in users. If you only want to update matching rows, then you can continue on the subquery path using IN or EXISTS in the WHERE clause. Alternatively, use JOIN:
UPDATE users u JOIN
pages p
ON p.user_id = u.id
SET u.page = p.page_name;
But most importantly, ask the existential question: Why do you need to do this update? You have a link between the two tables. Use the link instead of storing the name:
select u.*, p.page_name
from users u left join
pages p
on p.user_id = u.id;
You can use the below sample SQL and change as per your requirement.The Code above seems correct .Could you please paste the error
UPDATE TableB
SET TableB.value = (
SELECT TableA.value
FROM TableA
WHERE TableA.name = TableB.name
);

Mysql request SELECT with LEFT JOIN

I am a beginner in MySQL, I had some courses in mysql that are now finished, I have a homework , provided by our teacher, to make for my training but I block to retrieve data from the base for a social network site. I understand the basis of JOIN but I still have trouble understanding the logic of LEFT JOIN OR OTHER type INNER JOIN ...
Here is my problem, I have a database with 2 tables,
member(id_member*, login, photo)
friend(id_member_request*, id_member_accept*, accept, date_acceptation)
The accept field of the friend table is a field that allows me to validate if they have friends by setting the value to 1 instead of 0.
The fields id_member_request and id_member_accept agree to the id_member of the member table.
I want to retrieve the login and the picture of the members who are friends, to be able to display them then.
I tested several queries:
SELECT m.login
, m.photo
FROM friend AS a
LEFT
JOIN member AS m
ON m.id_member = a.id_member_accept
LEFT
JOIN member AS m1
ON m1.id_member = a.id_member_request
WHERE accept = 1;
The query works but does not show all friendly members and even several times some people.
In RIGHT JOIN The result is NULL.
In INNER JOIN no result.
Thanks in advance because I blocked for several hours and I confess to lose myself a little. ;-)
Formatting the statement will help you :)
Okay, so you are joining the same column on both m and m1.
The following will bring back the data, but you have to pass in id_member_request..
DECLARE INT #MEMBERID = 2; --Example ID
SELECT m.login, m.photo
FROM friend AS a
LEFT JOIN member AS m ON m.id_member = a.id_member_accept
WHERE a.id_member_request = #MEMBERID
AND a.accept = 1;
This is bring back the m.login, m.photo for all of the friends for the Member with the ID #MEMBERID;
I think you want the friends of person who logged in. means for particular Member_Id here is query that will help you
select * from friend a
inner join member b on (a.id_member_request=b.id_member or a.id_member_accept=b.id_member) and b.id_member=1
where accept=1
where 1 is a member id whose friends will be displayed
You could re-write your query as in the below example
SELECT `m`.`id_member` AS `memberID`,
`m`.`login` AS `memberLoginName`,
`m`.`photo` AS `memberPhoto`,
`m1`.`id_member` AS `friendID`,
`m1`.`login` AS `friendLoginName`,
`m1`.`photo` AS `friendPhoto`,
`a`.`accept_date` AS `acceptDate`
FROM `friends` AS `a`
INNER JOIN `member` AS `m` ON `a`.`id_member_request` = `m`.`id_member`
INNER JOIN `member` AS `m1` ON `a`.`id_member_accept` = `m1`.`id_member`
WHERE `a`.`accept`=1
ORDER BY `a`.`id_member_request`,`a`.`id_member_accept` ASC
View this example in SQL fiddle
Thanks a lot for your help ;-)
I've tried all your answers and with a little change, this one works fine even in INNER, LEFT, RIGHT JOIN...
SELECT login,photo
FROM friend AS a
INNER JOIN member AS m ON ( a.id_member_request = m.id_member
OR a.id_member_accept = m.id_member )
WHERE m.id_member !=$id_member
AND a.accept=1;

Is it possible with MySQL to generate a query to fetch from several tables even if one has no results?

I'm facing a problem here:
I'm building a forum, this forum has several tables and I'm trying to fetch the comments and user info in a single query.
So far, it should be easy, the problem is that I can't change the structure and with the following query I get a perfect result IF there is a like to the answer. If no one likes the answer it fails.
Select
mfr.mfr_forum_answers.id,
mfr.mfr_forum_answers.date_created,
mfr.mfr_forum_answers.last_updated,
mfr.mfr_forum_answers.content,
mfr.mfr_forum_answers.accepted,
mfr.mfr_forum_answers.user_id,
mfr.mfr_users.level,
mfr.mfr_users.avatar,
mfr.mfr_forum_likes.subject_id,
mfr.wp_users.ID As ID1,
mfr.mfr_forum_topics.user_id As owner_id,
(SELECT count(mfr.mfr_forum_likes.id) FROM mfr.mfr_forum_likes WHERE mfr.mfr_forum_likes.subject_id = :id AND mfr.mfr_forum_likes.type = 'answer') as likes,
(SELECT count(mfr.mfr_forum_likes.id) FROM mfr.mfr_forum_likes WHERE mfr.mfr_forum_likes.subject_id = :id AND makefitreal.mfr_forum_likes.type = 'answer' AND mfr.mfr_forum_likes.user_id = :sessionId ) as i_like,
mfr.wp_users.user_nicename
From
mfr.mfr_forum_likes Inner Join
mfr.mfr_forum_answers
On mfr.mfr_forum_answers.topic_id =
mfr.mfr_forum_likes.subject_id Inner Join
mfr.mfr_users
On mfr.mfr_forum_answers.user_id = mfr.mfr_users.id
Inner Join
mfr.wp_users
On mfr.mfr_users.id = mfr.wp_users.ID Inner Join
mfr.mfr_forum_topics
On mfr.mfr_forum_answers.topic_id = mfr.mfr_forum_topics.id
Where
mfr.mfr_forum_answers.topic_id = :id
And
mfr.mfr_forum_likes.type = 'answer'
So far as said it returns only if an answer has a like, I'm thinking on adding a add to the user who posts the answer by default but I'm trying to improve my skills by solving new issues.
If someone has a suggestion in how I could overcome the fact that if a table is empty, the query continues I'd be really thankfull.
Thanks in advance-
Pihh
Yes. What you are looking for are called left and right joins. According to the documentation, with a LEFT JOIN you still join two tables as normal but
If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table.
This means that you can try to join two tables, but if a row does not have any results it will still return the results from the first table. The same is true for a RIGHT JOIN only it works the opposite way: it will return results if the tabled being joined to has results, but the original table does not.
It looks like you have 3 tables for 3 relationships: there are answers, a user gives an answer, and an answer might or might not have like. To grab this data, I would suggest starting from your answers table, performing an INNER JOIN on your users table (assuming there are always users), and a LEFT JOIN on your likes table. Here is a simple example:
SELECT *
FROM answers
INNER JOIN users ON users.id = answers.user_id
LEFT JOIN likes ON likes.answer_id = answer.id
WHERE answers.id = :id
AND likes.type = 'answers'
Of course, if for some unknown reason you need to start from your likes table, then you'd have to RIGHT JOIN the other tables. I hope that gives you a good idea of how you'd make your query.

Reverse effect of JOIN

I got two tables :
A list of people;
A list of people I want to ignore.
When I read the list of people, I don't want to see the ignored people in the list.
My current solution is to query a second time the database (to select the people I want to ignore) and remove them from the array I create with PHP. It's working and it's fine.
However, I want to do that in MySQL. I know JOIN will join only if the row exists in the other table. I am looking for something different (won't show the entry IF the row exists).
I have searched in Google but the lack of "keywords" for this gave me no results.
Thanks
SELECT * FROM Person
LEFT OUTER JOIN IgnoredPerson
ON Person.id = IgnoredPerson.id
WHERE IgnoredPerson.id IS null
Explanation:
Exclude the records we don't want from the right side via a where clause
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Without knowing your schema, I'd suggest something along these lines:
SELECT * FROM people WHERE id NOT IN (SELECT person_id FROM ignored_people)
You could try something like this
SELECT * FROM people p WHERE NOT EXISTS (SELECT i.id FROM ignorePeople i where p.id = i.id )
here's a link about EXISTS in MySql

Categories