adrianle Posted February 18, 2015 Share Posted February 18, 2015 I've got a select statement using UNIONs between four tables so that I can pull records that have no relation to each other. The problem is each table has a different number of fields, so the UNIONs will fail. Is there a different/better way for me to pull unrelated recordsets from 4 different tables without having 4 different select statements? Quote Link to comment https://forums.phpfreaks.com/topic/294712-unions-and-field-quantity/ Share on other sites More sharing options...
Barand Posted February 18, 2015 Share Posted February 18, 2015 If you do a union like that then select dummy fields in the table with less data SELECT a, b, c FROM tablea UNION SELECT x, y, NULL as c FROM tableb 1 Quote Link to comment https://forums.phpfreaks.com/topic/294712-unions-and-field-quantity/#findComment-1506068 Share on other sites More sharing options...
adrianle Posted February 18, 2015 Author Share Posted February 18, 2015 Thanks, I'd heard about this too.. so the fields themselves don't matter, just the quantity of fields matters? This would suggest that I'd need to 'pad' all union'ed select statements to match the largest table in the group? Quote Link to comment https://forums.phpfreaks.com/topic/294712-unions-and-field-quantity/#findComment-1506071 Share on other sites More sharing options...
Barand Posted February 18, 2015 Share Posted February 18, 2015 The fields in each column should be of matching types too Quote Link to comment https://forums.phpfreaks.com/topic/294712-unions-and-field-quantity/#findComment-1506072 Share on other sites More sharing options...
adrianle Posted February 18, 2015 Author Share Posted February 18, 2015 Ouch, so that means that even with the extra false columns in the select, this will not work because there might be different column types in each table. Quote Link to comment https://forums.phpfreaks.com/topic/294712-unions-and-field-quantity/#findComment-1506094 Share on other sites More sharing options...
Barand Posted February 18, 2015 Share Posted February 18, 2015 Perhaps this is an occasion for separate queries. How different are the tables? Quote Link to comment https://forums.phpfreaks.com/topic/294712-unions-and-field-quantity/#findComment-1506095 Share on other sites More sharing options...
Psycho Posted February 19, 2015 Share Posted February 19, 2015 Do you need ALL the data from the tables? Just determine what fields you need and line up the ones that are of the same type and create dummy fields for the ones that don't line up. You sure as heck shouldn't be using SELECT * anywhere Quote Link to comment https://forums.phpfreaks.com/topic/294712-unions-and-field-quantity/#findComment-1506098 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.