brown2005 Posted January 25, 2011 Share Posted January 25, 2011 table 1 id url table 2 id domain user how can i select records from table 1, where table1.id is in table2.domain and user=1 is it SELECT * FROM table1, table2 WHERE table1.id=table2.domain AND table2.user=1 or is there a better/faster way? thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/225662-select-results-from-table1-where-tableid-is-in-table2/ Share on other sites More sharing options...
brown2005 Posted January 26, 2011 Author Share Posted January 26, 2011 WHERE table1.id IN (SELECT table2.domain FROM table2 WHERE table2.user='1') with regards to the above, is this the correct way of doing it? Quote Link to comment https://forums.phpfreaks.com/topic/225662-select-results-from-table1-where-tableid-is-in-table2/#findComment-1165383 Share on other sites More sharing options...
mikosiko Posted January 26, 2011 Share Posted January 26, 2011 if your table2.domain field hold the relationship with table1 (means table1.id = table2.domain) then your first select is the way to do it.. also you can write that in this way per example: SELECT table1.*, table2.id FROM table1 JOIN table2 ON table1.id = table2.domain AND table2.user = 1 you can also use a LEFT JOIN depending of your data and expected results Quote Link to comment https://forums.phpfreaks.com/topic/225662-select-results-from-table1-where-tableid-is-in-table2/#findComment-1165415 Share on other sites More sharing options...
brown2005 Posted January 26, 2011 Author Share Posted January 26, 2011 ok thanks. so it does not matter which way? is either of them faster or better to use than the other? Quote Link to comment https://forums.phpfreaks.com/topic/225662-select-results-from-table1-where-tableid-is-in-table2/#findComment-1165421 Share on other sites More sharing options...
fenway Posted January 28, 2011 Share Posted January 28, 2011 The JOIN is often better for performance. Quote Link to comment https://forums.phpfreaks.com/topic/225662-select-results-from-table1-where-tableid-is-in-table2/#findComment-1166350 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.