cs.punk Posted July 9, 2012 Share Posted July 9, 2012 Hello. Here's my problem. Table 1: height | id | |name | age 130 | 1 | Josh | 18 135 | 2 | Jack | 17 155 | 3 | Jody | 19 Table 2: id | name | gender | height 1 | BOB | MALE | 140 I'd like to 'combine' the tables into 2, and sort them by height. When data isn't available the columns can be null. I'm finding it hard to relate them with the height table however, joins seem to have a 'direct column' relationship. Ideally it'd be all in one table but I'd have to rewrite a large chunk of the system potentially, hoping theres an easier way? Potential output: height | T1.id | T1.name | T1.age | T2.id | T2.name | T2.gender | 130 | 1 | Josh | 18 | NULL | NULL | NULL 135 | 2 | Jack | 17 | NULL | NULL | NULL 140 | NULL | NULL | NULL | 1 | BOB | MALE 155 | 3 | Jody | 19 | NULL | NULL | NULL Heres my SQL query i've got so far SELECT t1.*, t2.* FROM t1 LEFT JOIN t2 ON t1.height = t2.height ORDER BY t1.height LIMIT 0, 30 Link to comment https://forums.phpfreaks.com/topic/265454-join-two-tables-with-common-comulmns-and-sort-by-field-name/ Share on other sites More sharing options...
cs.punk Posted July 9, 2012 Author Share Posted July 9, 2012 A mate has just helped me out on this. Gotta learn some advanced SQL me thinks. Heres the query: SELECT height, id, name, age, NULL as 't2.id', NULL as t2.name, NULL as t2.gender FROM t1 UNION SELECT height, NULL, NULL, NULL, id, name, gender FROM t2 ORDER BY height Link to comment https://forums.phpfreaks.com/topic/265454-join-two-tables-with-common-comulmns-and-sort-by-field-name/#findComment-1360429 Share on other sites More sharing options...
ezekielnin Posted July 10, 2012 Share Posted July 10, 2012 I hope the union works for you. I don't think joining those two tables might work. Usually you join 2 tables on a unique identifier. Link to comment https://forums.phpfreaks.com/topic/265454-join-two-tables-with-common-comulmns-and-sort-by-field-name/#findComment-1360454 Share on other sites More sharing options...
hakimserwa Posted July 10, 2012 Share Posted July 10, 2012 In the first place i dont see why you even need to seperate these tables. its pretty obvious that all information submited here can be for a single person. there is no need for a relational dataase. just create one table insert all the data and use sql to call only the data with a particular height value. Link to comment https://forums.phpfreaks.com/topic/265454-join-two-tables-with-common-comulmns-and-sort-by-field-name/#findComment-1360482 Share on other sites More sharing options...
cs.punk Posted July 10, 2012 Author Share Posted July 10, 2012 In the first place i dont see why you even need to seperate these tables. its pretty obvious that all information submited here can be for a single person. there is no need for a relational dataase. just create one table insert all the data and use sql to call only the data with a particular height value. Indeed. However like stated, there already is a considerable chunk written. Perhaps it's cutting corners and isn't the best decision in terms of future modifications but I guess it'll have to do. Link to comment https://forums.phpfreaks.com/topic/265454-join-two-tables-with-common-comulmns-and-sort-by-field-name/#findComment-1360486 Share on other sites More sharing options...
Barand Posted July 12, 2012 Share Posted July 12, 2012 | . cost of additional | . effort c | . o | . s | . cost of rewrite t |---------.---------------------------------------- | . | . | . | . +------------------------------------------------- X time There could be point (X) in the future when you really wish you had made the effort to change the structure Link to comment https://forums.phpfreaks.com/topic/265454-join-two-tables-with-common-comulmns-and-sort-by-field-name/#findComment-1361014 Share on other sites More sharing options...
hakimserwa Posted July 12, 2012 Share Posted July 12, 2012 be straight it might be confusing Link to comment https://forums.phpfreaks.com/topic/265454-join-two-tables-with-common-comulmns-and-sort-by-field-name/#findComment-1361024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.