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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

    |                  . 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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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