Dimitris
Dimitris

Reputation: 2531

sqlalchemy order table by foreign key

I have a table which holds 2 foreign keys to another table:

Table A{
primary_key
foreign_key_1_to_table_B
foreign_key_2_to_table_B
}

Table B{
primary_key
a_character_column
}

Now I want to sort the rows of table A according to a_character_column but as this is referenced by foreign_key_1_to_table_B.

All the queries are implemented in sqlalchemy, but since this is a generic problem I'd rather use pseudo sql.

Any idea how to form the ORDER BY clause of the sql query?

I should add that I need a join between A and B for both foreign_key_1_to_table_B and foreign_key_2_to_table_B. The problem arises when I try to specify whether the ordering should be done with respect to the foreign_key_1_to_table_b or foreign_key_2_to_table_B.

Upvotes: 0

Views: 490

Answers (1)

bpgergo
bpgergo

Reputation: 16047

In SQL it'd be

select A.* 
from A 
inner join B on (A.foreign_key_1_to_table_B = B.primary_key
order by B.a_character_column

Upvotes: 2

Related Questions