![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi guys I have a problem in sorting rows for a table that is connected to a many-to-many table. Here is the structure: Table Person id, name insert into Person values(1, 'mark'); insert into Person values(2, 'paul'); Table City id, name insert into City values(1, 'new york'); insert into City values(2, 'london'); insert into City values(3, 'madrid'); Table City_Person id, city_id, person_id insert into City_Person values(1, 1, 1); insert into City_Person values(2, 3, 1); insert into City_Person values(3, 2, 2); insert into City_Person values(4, 3, 2); So, if i want to retrieve all the people and order them by city_name, how can I do this? The result should be: pauk mark As paul belongs to cities: london/madrid while mark belonbgs to cities: new york/madrid Alphabetically, london is the first one, before madrid and new york. You see the point? |
|
name | name | +------+----------+ paul | london | paul | madrid | mark | madrid | mark | new york | +------+----------+ |
|
name | name | +------+----------+ paul | london | mark | madrid | paul | madrid | mark | new york | +------+----------+ |
#3
| |||
| |||
|
|
On 09-11-10 10:40, pac Man wrote: Hi guys I have a problem in sorting rows for a table that is connected to a many-to-many table. Here is the structure: Table Person id, name insert into Person values(1, 'mark'); insert into Person values(2, 'paul'); Table City id, name insert into City values(1, 'new york'); insert into City values(2, 'london'); insert into City values(3, 'madrid'); Table City_Person id, city_id, person_id insert into City_Person values(1, 1, 1); insert into City_Person values(2, 3, 1); insert into City_Person values(3, 2, 2); insert into City_Person values(4, 3, 2); So, if i want to retrieve all the people and order them by city_name, how can I do this? The result should be: pauk mark As paul belongs to cities: london/madrid while mark belonbgs to cities: new york/madrid Alphabetically, london is the first one, before madrid and new york. You see the point? I dont understand your problem, Why do you want to list Persons, without the City... maybe next 2 queries help... mysql> select a.name, c.name from Person a inner join City_Person b on a.id=b.person_id inner join City c on c.id=b.city_id order by c.name; +------+----------+ | name | name * * | +------+----------+ | paul | london * | | paul | madrid * | | mark | madrid * | | mark | new york | +------+----------+ 4 rows in set (0.00 sec) mysql> select a.name, c.name from Person a inner join City_Person b on a.id=b.person_id inner join City c on c.id=b.city_id order by c.name, a.name; +------+----------+ | name | name * * | +------+----------+ | paul | london * | | mark | madrid * | | paul | madrid * | | mark | new york | +------+----------+ 4 rows in set (0.00 sec) -- Luuk |
#4
| ||||||
| ||||||
|
|
I have a problem in sorting rows for a table that is connected to a many-to-many table. Here is the structure: Table Person id, name insert into Person values(1, 'mark'); insert into Person values(2, 'paul'); Table City id, name insert into City values(1, 'new york'); insert into City values(2, 'london'); insert into City values(3, 'madrid'); Table City_Person id, city_id, person_id insert into City_Person values(1, 1, 1); insert into City_Person values(2, 3, 1); insert into City_Person values(3, 2, 2); insert into City_Person values(4, 3, 2); |
|
So, if i want to retrieve all the people and order them by city_name, how can I do this? |
|
The result should be: pauk mark As paul belongs to cities: london/madrid while mark belonbgs to cities: new york/madrid |
|
You need a LEFT JOIN, if there are persons without cities and you want to retrieve them also |
|
You must group your result, otherwise you would get each person as many times as it appears in City_Person |
|
As the result is grouped, you can now sort by the city for each person with the "earliest" name in alphabet. |
#5
| |||
| |||
|
|
On 9 Nov, 10:58, Luuk<L... (AT) invalid (DOT) lan> wrote: On 09-11-10 10:40, pac Man wrote: Hi guys I have a problem in sorting rows for a table that is connected to a many-to-many table. Here is the structure: Table Person id, name insert into Person values(1, 'mark'); insert into Person values(2, 'paul'); Table City id, name insert into City values(1, 'new york'); insert into City values(2, 'london'); insert into City values(3, 'madrid'); Table City_Person id, city_id, person_id insert into City_Person values(1, 1, 1); insert into City_Person values(2, 3, 1); insert into City_Person values(3, 2, 2); insert into City_Person values(4, 3, 2); So, if i want to retrieve all the people and order them by city_name, how can I do this? The result should be: pauk mark As paul belongs to cities: london/madrid while mark belonbgs to cities: new york/madrid Alphabetically, london is the first one, before madrid and new york. You see the point? I dont understand your problem, Why do you want to list Persons, without the City... maybe next 2 queries help... mysql> select a.name, c.name from Person a inner join City_Person b on a.id=b.person_id inner join City c on c.id=b.city_id order by c.name; +------+----------+ | name | name | +------+----------+ | paul | london | | paul | madrid | | mark | madrid | | mark | new york | +------+----------+ 4 rows in set (0.00 sec) mysql> select a.name, c.name from Person a inner join City_Person b on a.id=b.person_id inner join City c on c.id=b.city_id order by c.name, a.name; +------+----------+ | name | name | +------+----------+ | paul | london | | mark | madrid | | paul | madrid | | mark | new york | +------+----------+ 4 rows in set (0.00 sec) -- Luuk To be honest, I need to retrieve eveything, like you said. But Then Paul has to be shown only once. You get it? Something like: Paul - london/madrid Mark madrid/new york Right now Im using a bad solution, by getting all the cities for a person by doind a loop inside the loop. Maybe a solution can be getting everything and only show the person once? Dont know if it is the best solution though. |
#6
| |||
| |||
|
|
On 9 Nov, 10:58, Luuk <L... (AT) invalid (DOT) lan> wrote: On 09-11-10 10:40, pac Man wrote: Hi guys I have a problem in sorting rows for a table that is connected to a many-to-many table. Here is the structure: Table Person id, name insert into Person values(1, 'mark'); insert into Person values(2, 'paul'); Table City id, name insert into City values(1, 'new york'); insert into City values(2, 'london'); insert into City values(3, 'madrid'); Table City_Person id, city_id, person_id insert into City_Person values(1, 1, 1); insert into City_Person values(2, 3, 1); insert into City_Person values(3, 2, 2); insert into City_Person values(4, 3, 2); So, if i want to retrieve all the people and order them by city_name, how can I do this? The result should be: pauk mark As paul belongs to cities: london/madrid while mark belonbgs to cities: new york/madrid Alphabetically, london is the first one, before madrid and new york. You see the point? I dont understand your problem, Why do you want to list Persons, without the City... maybe next 2 queries help... mysql> select a.name, c.name from Person a inner join City_Person b on a.id=b.person_id inner join City c on c.id=b.city_id order by c.name; +------+----------+ | name | name * * | +------+----------+ | paul | london * | | paul | madrid * | | mark | madrid * | | mark | new york | +------+----------+ 4 rows in set (0.00 sec) mysql> select a.name, c.name from Person a inner join City_Person b on a.id=b.person_id inner join City c on c.id=b.city_id order by c.name, a.name; +------+----------+ | name | name * * | +------+----------+ | paul | london * | | mark | madrid * | | paul | madrid * | | mark | new york | +------+----------+ 4 rows in set (0.00 sec) -- Luuk To be honest, I need to retrieve eveything, like you said. But Then Paul has to be shown only once. You get it? Something like: Paul - london/madrid Mark madrid/new york Right now Im using a bad solution, by getting all the cities for a person by doind a loop inside the loop. Maybe a solution can be getting everything and only show the person once? Dont know if it is the best solution though. |
#7
| |||
| |||
|
|
On Tue, 9 Nov 2010 02:29:04 -0800 (PST), pac Man wrote: To be honest, I need to retrieve eveything, like you said. But Then Paul has to be shown only once. You get it? Something like: Paul - london/madrid Mark madrid/new york Ah, see... in order to sort this way, you don't really care about any city *other than* the first one alphabetically in the list. |
#8
| |||
| |||
|
|
Am 09.11.2010 15:55, schrieb Peter H. Coffin: On Tue, 9 Nov 2010 02:29:04 -0800 (PST), pac Man wrote: To be honest, I need to retrieve eveything, like you said. But Then Paul has to be shown only once. You get it? Something like: Paul - london/madrid Mark madrid/new york Ah, see... in order to sort this way, you don't really care about any city *other than* the first one alphabetically in the list. What do you mean by "other than the first one"? What about: Paul - london/madrid Mark - london/new york Here the first one is equal... Maybe I didn't get your point? Helmut |
#9
| |||
| |||
|
|
On 12-11-10 10:40, Helmut Chang wrote: Am 09.11.2010 15:55, schrieb Peter H. Coffin: On Tue, 9 Nov 2010 02:29:04 -0800 (PST), pac Man wrote: To be honest, I need to retrieve eveything, like you said. But Then ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|
Paul - london/madrid Mark madrid/new york .... Paul - london/madrid Mark - london/new york .... The OP did not specify how to sort Persons that have the same City as the first one in their list. |
.![]() |
| Thread Tools | |
| Display Modes | |
| |