Kinsbane Posted May 4, 2009 Share Posted May 4, 2009 Hi all! I have two different tables, with no common fields, to be queried in one query and have the query return one resultset. The reason for this is I am working on a web app that displays listings of files from both tables. I don't want to have two separate queries because then the data from one table will be given precedent over data from another table (I think).. I also don't want to have to use a CRON job to populate a separate table every hour, as these records get updated frequently. So, I am hoping there's a way to query two tables with no common fields to generate a single result set that I can then display to the end user. The version of MySQL I have for now is 3.23, although the new webserver we're installing has latest MySQL 5. Thank you very much in advance for any assistance! Quote Link to comment https://forums.phpfreaks.com/topic/156749-two-tables-with-two-different-structures-need-one-query/ Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 SELECT * FROM table1_name, table2_name ? Quote Link to comment https://forums.phpfreaks.com/topic/156749-two-tables-with-two-different-structures-need-one-query/#findComment-825407 Share on other sites More sharing options...
fenway Posted May 4, 2009 Share Posted May 4, 2009 It's called "UNION. Quote Link to comment https://forums.phpfreaks.com/topic/156749-two-tables-with-two-different-structures-need-one-query/#findComment-826114 Share on other sites More sharing options...
Kinsbane Posted May 4, 2009 Author Share Posted May 4, 2009 Looks like UNION is the way I need to go, but MySQL 3.xx doesn't support it, as we're not using our PHP/MySQL5 server yet. I found this link as a work-around: www.codewalkers.com/c/a/Database-Articles/Writing-UNION-statements-in-MySQL-3x/ but I can't seem figure out where I put in my conditionals for which rows I want to extract from each table... Quote Link to comment https://forums.phpfreaks.com/topic/156749-two-tables-with-two-different-structures-need-one-query/#findComment-826308 Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Problem with union is that it will fail if both tables do not have the same number of columns. Quote Link to comment https://forums.phpfreaks.com/topic/156749-two-tables-with-two-different-structures-need-one-query/#findComment-826315 Share on other sites More sharing options...
fenway Posted May 5, 2009 Share Posted May 5, 2009 Problem with union is that it will fail if both tables do not have the same number of columns. You can easily work around that... use a cartesan join in definitely worse. Quote Link to comment https://forums.phpfreaks.com/topic/156749-two-tables-with-two-different-structures-need-one-query/#findComment-826724 Share on other sites More sharing options...
Kinsbane Posted May 8, 2009 Author Share Posted May 8, 2009 Problem with union is that it will fail if both tables do not have the same number of columns. You can easily work around that... use a cartesan join in definitely worse. Could you explain a bit further fenway? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/156749-two-tables-with-two-different-structures-need-one-query/#findComment-829689 Share on other sites More sharing options...
fenway Posted May 8, 2009 Share Posted May 8, 2009 Well, what do you have now? Quote Link to comment https://forums.phpfreaks.com/topic/156749-two-tables-with-two-different-structures-need-one-query/#findComment-829741 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.