Zeest Posted July 4, 2006 Share Posted July 4, 2006 I have this situation where I want to replace a delete query in a loop with one mass query. The problem that I face, I will try to outline here.There are 3 tablesa simple mock up....[code]table_1 table_2 table_3id id id1 1 22 2 33 5 45 6 57 8 69 7 8 9 10[/code]Now here's what I want to do...Join tables table_1 and table_2 first, using id so that I get an intersection of ids in the tablesagain an example[code]result of join 1table_1 table_2id other_table_1_data... id other_table_2_data...1 12 23 NULL5 5NULL 67 NULLNULL 89 NULL[/code]As you can see a simple LEFT/RIGHT join wouldn't help since I'll only get all rows in one table for that but what I want is an intersection of rows in tables 1 and 2.The next part is relatively simple and I guess I have that figured out, I need to compare this result table with table_3 to see which records in the result Set are not present in table_3 so a simple LEFT join outta do it.Then I need to delete rows from table_1 and table_2 that are not present in table_3. I can do this separately using table 1 and 3 on left join and then table 2 and 3, but doing them in one query could definitely save resources since it runs every 15 minutes on my already overloaded server. Quote Link to comment https://forums.phpfreaks.com/topic/13630-intersection-joins/ Share on other sites More sharing options...
effigy Posted July 4, 2006 Share Posted July 4, 2006 I haven't upgraded my MySQL at home, so I cannot test this out--would a union work?[code] (select t1.id, t2.id from table_1 t1 left join table_2 t2 on t1.id = t2.id)union(select t1.id, t2.id from table_1 t1 right join table_2 t2 on t1.id = t2.id)[/code] Quote Link to comment https://forums.phpfreaks.com/topic/13630-intersection-joins/#findComment-52931 Share on other sites More sharing options...
Zeest Posted July 4, 2006 Author Share Posted July 4, 2006 Thanks that code outputs exactly the table I've shown in step 2. The problem now though is that I don't know how I'm supposed to LEFT JOIN table_3 to the results table achieved by this query since it has union and all and I'm not that familiar with this syntax.The output should be somewhat like this[code]table_1 table_2 table_31 1 NULL2 2 23 NULL 35 5 5NULL 6 67 NULL 7NULL 8 89 NULL 9[/code]what I want basically is(those rows present in table1 union table2) but not in (table3)so once I get the above result table all I have to do is add a WHERE table_3.id IS NULLin this case that row is 1This row is to be deleted from table_1 and table_2 in one query.another exampletable1: 3, 6, 9, 10table2: 1, 3, 5, 7table3: 8, 10from table1 3,6,9 should be deletedfrom table2 1,3,5,7 should be deletedI'm referring to cross table delete http://www.electrictoolbox.com/article/mysql/cross-table-delete/ Quote Link to comment https://forums.phpfreaks.com/topic/13630-intersection-joins/#findComment-52981 Share on other sites More sharing options...
effigy Posted July 4, 2006 Share Posted July 4, 2006 You'll need a temporary table. Try something like this...[code]create temporary table table_name (select ...) union (select ...);[/code]...then use the temporary table in a select statement as you would any other and join it to table 3. Quote Link to comment https://forums.phpfreaks.com/topic/13630-intersection-joins/#findComment-53001 Share on other sites More sharing options...
Zeest Posted July 4, 2006 Author Share Posted July 4, 2006 Whats the life time of a temporary table? I assume that if I create a temp table in one mysql_query, then I have to do another query to use that table, isn't it?so something like[code]mysql_query("create temporary table abc{(select t1.id, t2.id from table_1 t1 left join table_2 t2 on t1.id = t2.id)union(select t1.id, t2.id from table_1 t1 right join table_2 t2 on t1.id = t2.id)}");mysql_query("DELETE table_1.*, table_2.*FROM abcLEFT JOIN table_3 ON abc.id=table_3.idWHERE table_3.id IS NULL")[/code]should work.....? Quote Link to comment https://forums.phpfreaks.com/topic/13630-intersection-joins/#findComment-53006 Share on other sites More sharing options...
effigy Posted July 4, 2006 Share Posted July 4, 2006 From the [url=http://dev.mysql.com/doc/refman/5.0/en/create-table.html]MySQL 5.0 Manual[/url]:[quote]You can use the TEMPORARY keyword when creating a table. A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name. (The existing table is hidden until the temporary table is dropped.) To create temporary tables, you must have the CREATE TEMPORARY TABLES privilege.[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/13630-intersection-joins/#findComment-53014 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.