Jump to content

Join two tables with common comulmns and sort by field name


cs.punk

Recommended Posts

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

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

 

:D

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.

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.

    |                  . 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   

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.