Results 1 to 3 of 3

Thread: ORDER BY (field in foreign table)

  1. #1
    Join Date
    Apr 2008
    Posts
    2

    ORDER BY (field in foreign table)

    Newbie question. Can someone show me an SQL statement that sorts the results of a query by a field in a different table?

    CREATE TABLE `Table1`
    (
    `table1_id` INTEGER AUTO_INCREMENT ,
    `table1_name` VARCHAR(255),
    PRIMARY KEY (`table1_id`)
    )

    CREATE TABLE `Table2`
    (
    `table2_id` INTEGER AUTO_INCREMENT ,
    `table2_name` VARCHAR(255),
    `table2_table1` INTEGER,
    PRIMARY KEY (`table2_id`)
    )

    ALTER TABLE `Table2` ADD FOREIGN KEY (`table2_table1`) REFERENCES `Table1`(`table1_id`);

    What I'd like is something like:

    SELECT * FROM Table2 ORDER BY "Table1(table2_table1).tabel1_name",table2_nam e

    It's the section in double quotes that I can't figure out how to compose.

    As an example, if Table 1 has
    1, A
    2, B
    3, C

    and Table 2 has
    1, a, 2
    2, b, 1
    3, c, 2

    then I'd like the sort to return
    2, b, 1
    1, a, 2
    3, c, 2


    TIA,

    Stephen

  2. #2
    Join Date
    Apr 2008
    Posts
    1

    Use Join

    Use join between the two tables

    select table2_id,table2_name,table2_table1 from table2,table1 where table1.table1_id=table2.table2_table1 order by table1.table1_id,table2.table2_id

  3. #3
    Join Date
    Apr 2008
    Posts
    2
    Thanks, Perfect!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •