sspoke Posted July 30, 2010 Share Posted July 30, 2010 Hello man, I'm trying to make this process in a single query without doing like SELECT * FROM users WHERE ping < DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 15 SECOND) Go threw each $row['id'] mysql_query("DELETE FROM users WHERE userId = $row['id']"); mysql_query("DELETE FROM chats WHERE userId = $row['id']"); mysql_query("DELETE FROM chats WHERE randomUserId = $row['id']"); I already optmized to skip some php load of users table but i can't seem to link it up to chat's table since chat table also has randomUserId and userId both being possible candidates for deleting. Got ATM mysql_query("DELETE FROM users WHERE ping < DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 15 SECOND)"); how i join chats table too is my question for deleting in single query. Thank you Link to comment https://forums.phpfreaks.com/topic/209379-sql-ping-delete-from-different-tables-single-query/ Share on other sites More sharing options...
sspoke Posted July 30, 2010 Author Share Posted July 30, 2010 Came up with this DELETE FROM users,chats WHERE users.ping < DATE_SUB(CURRENT_TIMESTAMP(), INTERVAL 15 SECOND) OR users.id = chats.userId OR users.id=chats.randomUserId but it doesn't look right. Because note the users would exist but the chats might either not have userId or randomUserId or maybe both missing. Link to comment https://forums.phpfreaks.com/topic/209379-sql-ping-delete-from-different-tables-single-query/#findComment-1093291 Share on other sites More sharing options...
fenway Posted August 4, 2010 Share Posted August 4, 2010 Then you'd need a left join. Link to comment https://forums.phpfreaks.com/topic/209379-sql-ping-delete-from-different-tables-single-query/#findComment-1095211 Share on other sites More sharing options...
sspoke Posted August 6, 2010 Author Share Posted August 6, 2010 can someone do this for me just saying i need a left join won't help me that much sorry if this makes anyone angry but i am one of these simple thousand query coder who wishes that sometime someone can provide me with something awesome as a one liner.. btw just realized that there is another query i have to run to delete from mysql_query("DELETE FROM typing WHERE id = $row['id']"); which makes it now mysql_query("DELETE FROM users WHERE userId = $row['id']"); mysql_query("DELETE FROM chats WHERE userId = $row['id']"); mysql_query("DELETE FROM chats WHERE randomUserId = $row['id']"); mysql_query("DELETE FROM typing WHERE id = $row['id']"); 3 different table accessors will that require 2 left joins? Link to comment https://forums.phpfreaks.com/topic/209379-sql-ping-delete-from-different-tables-single-query/#findComment-1095849 Share on other sites More sharing options...
sspoke Posted August 6, 2010 Author Share Posted August 6, 2010 DELETE users.*, chats.*, typing.* FROM users u, chats c, typing t WHERE u.userId = $row['id'] AND c.userId = $row['id'] AND c.randomUserId = $row['id'] AND t.id = $row['id'] like that? but some places *could* be already empty like typing/chats. i've looked into LEFT JOIN seems it only joins one other table how can I do 3 at once? To delete data from all 3 tables based on that one row[id] variable Link to comment https://forums.phpfreaks.com/topic/209379-sql-ping-delete-from-different-tables-single-query/#findComment-1095853 Share on other sites More sharing options...
fenway Posted August 8, 2010 Share Posted August 8, 2010 Then you haven't looked enough. DELETE u.*, c.*, c2.*, t.* FROM users u LEFT JOIN chats c ON c.userId = $row['id'] LEFT JOIN chats c2 ON c.randomUserId = $row['id'] LEFT JOIN typing t ON t.id = $row['id'] WHERE u.userId = $row['id'] Link to comment https://forums.phpfreaks.com/topic/209379-sql-ping-delete-from-different-tables-single-query/#findComment-1096650 Share on other sites More sharing options...
sspoke Posted August 8, 2010 Author Share Posted August 8, 2010 Thanks fenway you're awesome, very smart man. Works like a charm! Although if a user's userId is already deleted all the other queries fail but i doubt that will happen any other way except if I edit the database by hand. Link to comment https://forums.phpfreaks.com/topic/209379-sql-ping-delete-from-different-tables-single-query/#findComment-1096723 Share on other sites More sharing options...
sspoke Posted August 8, 2010 Author Share Posted August 8, 2010 You did mean DELETE u.*, c.*, c2.*, t.* FROM users u LEFT JOIN chats c ON c.userId = $row['id'] LEFT JOIN chats c2 ON c2.randomUserId = $row['id'] LEFT JOIN typing t ON t.id = $row['id'] WHERE u.userId = $row['id'] right? c2.randomUserId not c.randomUserId ? Link to comment https://forums.phpfreaks.com/topic/209379-sql-ping-delete-from-different-tables-single-query/#findComment-1096744 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.