UPDATE comunications,
files
INNER JOIN user_join
ON user_join.file_id = files.file_id
SET files.file_status = 1,
comunications.post_status = 1
WHERE comunications.post_id = 18
AND user_join.post_id = comunications.post_id
This is my join table:
The query works fine to update the file in the files table if file_id is not null, but not when file_id is null so, how can adapt this query to update the value in the files table only if file_id is not null? This is what I’ve tried so far:
UPDATE comunications,
files
INNER JOIN user_join
ON user_join.file_id = files.file_id
SET files.file_status = IF(user_join.file_id IS NOT NULL, 1, files.file_status),
comunications.post_status = 1
WHERE comunications.post_id = 18
AND user_join.post_id = comunications.post_id
Back of my head’s saying files.file_status = CASE WHEN user_join.file_id IS NULL THEN files.file_status ELSE 1 END
It’s also 5 am. The back of my head is also questioning why an inner join would have NULL in an ID field.
Hi @m_hutley thanks for your reply, if I try what you sugested then I get the following error:
Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect
I can make it work with two queries a select query first (to check if user_join.user_id is null or not) and then an update query.
The reason why the user_join.file_id is null is because I use the user_join table to connet a many to many relation between the comunications and the files tables and because the comunication can have a file attached or not
Hi @r937 thanks for your answer I understand now, so basically I should create two join tables. Just a quick thought if the post_id is primary key doesn’t mean it can’t accept duplicates? What happen in a situation where the same post (same ID) is shared with two users?
Hi @r937 sorry to trouble you again, I forgot to ask another question. When I create a new communication I can share the communication with one or many users but also with a group of users for example if I have some users with client role and some users with seller role and I want to share the communication with all the sellers and just one or more clients, how can I manage this situation? You can see in the image I’ve posted some user_id are NULL Do I need a third join table? Many thanks
depends entirely on whether you have an actual database structure for user groups
in which case your communications-to-users many-to-many table would link communications to user structure
google supertables and subtables for more information
for simplicity, i would consider just linking communications to individual users, and generating the users list at the time of communication – that way, when the user structure changes (and it will), you have an accurate picture of who got the communication, not who’s in the structure now
Hi @r937 so far I’ve got a table with all the roles and then a user_role table wich is a join table with the columns (user_id and role_id).
This is possible, but what appen if I register a new user with seller role then he will not be able to see the comunications that I’ve shared with all the sellers.
I would like if I create a new user with seller role then he will be able to see all communications already shared with other sellers and also all comunications shared for him (obiously these will not be in the database yet) I’ve come up with the following schema but not sure if it will work, (sorry it is a hand sketch)