0

how I can follow the order of a column according to their foreign key?

for example: i have two tables (Country and State)

country(id, country)
state(id_country, id_state, state) this is where i have the problem...

I want to keep the order by country with a sequence

1 | 1 | first state in the country one
1 | 2 | second state in the country one
1 | 3 | third state in the country one
2 | 1 | first state in the country two

this is possible?

Nelson Jean Pierre
  • 281
  • 1
  • 3
  • 10
  • What do you mean by "keep the order"? Usually, this kind of result is achieved by querying your database, and specifying an `ORDER BY` clause to direct the sorting order of the result set. In your example, the tables would be `JOIN`d together in the query before sorting. Sorting first by country, then by state would achieve the output you desire. – scottb Feb 19 '15 at 00:53
  • 1
    Modify the question so that it was obvious what exactly you need to achieve. – zerkms Feb 19 '15 at 02:15
  • I looked in other questions .. but I need to know how to do the insert to follow the counting of states according to country? I got this, but no solution http://stackoverflow.com/questions/3591298/how-to-create-a-postgresql-partitioned-sequence?rq=1 – Nelson Jean Pierre Feb 19 '15 at 02:41
  • Did you see this one http://stackoverflow.com/questions/4672629/postgresql-company-id-based-sequence ? There is a solution there, although it could have concurrency issues. – harmic Feb 19 '15 at 04:05
  • It does not matter the order you use to insert the rows. There is no guarantee they will be retrieved in that order unless you use the `order by` clause. – Clodoaldo Neto Feb 19 '15 at 04:40
  • actually interest me for to insert regardless of the order that will, after i have a query and filter – Nelson Jean Pierre Feb 19 '15 at 17:50

0 Answers0