scrubbicus Posted May 18, 2009 Share Posted May 18, 2009 So I'm creating a search engine on my website and I want to be able to select all from two tables. I tried this but it doesnt work. SELECT * FROM content AND articles Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/ Share on other sites More sharing options...
Ken2k7 Posted May 18, 2009 Share Posted May 18, 2009 Well, without further information, I can only provide a cartesian join - SELECT * FROM content, articles Though I don't recommend that method. It's pretty slow. Use JOIN instead. Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-836196 Share on other sites More sharing options...
luca200 Posted May 19, 2009 Share Posted May 19, 2009 Not only it's slow, it's totally senseless. Probably you should use UNION Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-837029 Share on other sites More sharing options...
Ken2k7 Posted May 19, 2009 Share Posted May 19, 2009 Not only it's slow, it's totally senseless. Probably you should use UNION That only works if you're selecting the same number of columns from each table, which is not always the case. Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-837176 Share on other sites More sharing options...
luca200 Posted May 19, 2009 Share Posted May 19, 2009 Not only it's slow, it's totally senseless. Probably you should use UNION That only works if you're selecting the same number of columns from each table, which is not always the case. Of course, but that's a formal point, and you can find a workaround to it. But if your tables are not related, surely you can't join them. And this is a logic point, no workaround. Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-837185 Share on other sites More sharing options...
scrubbicus Posted May 19, 2009 Author Share Posted May 19, 2009 So I should use UNION or JOIN I'll look up those commands on the MySQL website and learn more about those.? Are those the only two ways I can do what I'm trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-837343 Share on other sites More sharing options...
fenway Posted May 19, 2009 Share Posted May 19, 2009 So I should use UNION or JOIN I'll look up those commands on the MySQL website and learn more about those.? Are those the only two ways I can do what I'm trying to do? Well, are they "related" to each other or just "duplicates"? Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-837352 Share on other sites More sharing options...
scrubbicus Posted May 20, 2009 Author Share Posted May 20, 2009 These two tables are not related to each other. One is where all the articles are posted and the other is where I store all my content information for the different pages. I just want to be able to SELECT ALL from each table preferably a different column name for both but I can definitely change the column name so that the 'content' column is the same for both. So I'd be selecting two different tables with a column that has the same name. Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-838491 Share on other sites More sharing options...
luca200 Posted May 21, 2009 Share Posted May 21, 2009 I don't understand if you need all the columns in the tables or just one for each table. In the second case you can do something like SELECT column1 FROM table1 UNION all SELECT column2 FROM table2 and you'll get all you want under the name 'column1' Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-838802 Share on other sites More sharing options...
paparts Posted May 21, 2009 Share Posted May 21, 2009 This should do the trick: SELECT * FROM content a, articles b Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-838854 Share on other sites More sharing options...
fenway Posted May 22, 2009 Share Posted May 22, 2009 This should do the trick: SELECT * FROM content a, articles b No, it won't -- that's a cartesian join. Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-840183 Share on other sites More sharing options...
paparts Posted May 29, 2009 Share Posted May 29, 2009 But why? I tried it and it worked... Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-845132 Share on other sites More sharing options...
kickstart Posted May 29, 2009 Share Posted May 29, 2009 Hi It would join every row together. Ie, say one table had rows with a column values 1, 2 and 3, while the other had values 7, 8 and 9, you would get returned 9 rows rather than 6, each with 2 columns giving 1,7 / 1,8 / 1,9 / 2,7 / 2,8 / 2,9 / 3,7 / 3,8 / 3,9. As the number of rows on the originals tables increased the number of returned rows would go through the roof. It can be useful if you want every combination of rows between 2 tables, but almost certainly not in this situation. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/158542-select-from-two-tables/#findComment-845158 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.