1

我的网络应用程序中有一个(我想)ORDER BY 问题。没有其他问题/答案似乎与我的相同,或者我不能适用于我的问题。我将尝试用一个可以解决我的问题的简单案例来尽可能地解释自己。

想象一下 3 个表,“客户”“地址”和“客户地址”。

**Customers**
id,  name,      surname
1,  "John",    "Willis"
2,  "Sarah",   "Davies"
3,  "Mark",    "Jones"
4,  "Linda",   "Ede"

**Address**
id_dir, address
1,      "some adress"
2,      "other address"
3,      "holliday address"
4,      "my other house address"
5,      "Flat in the beach address"

**CustomersAdress**
id_ca, id_customer, id_address
1,         1,       4
2,         2,       4
3,         3,       1
4,         1,       2
5,         2,       2
6,         4,       5
7,         4,       3

很明显,CustomersAdress 是一个将客户与他们拥有的地址相关联的表。

通过一个简单的 INNER JOIN,我可以获得以下所有关系:

SELECT id_customer, surname, name, id_dir, address
FROM CustomersAddress
INNER JOIN Customers ON Customers.id=CustomerAddress.id_customer
INNER JOIN Address ON Address.id_dir=CustomerAddress.id_address

有了这些,我可以用 ORDER BY 订购,例如,如下

ORDER BY surname

甚至

ORDER BY surname, id_dir / ORDER BY surname, id_dir

使用“ORDER BY id_dir, surname”示例我会得到:

idcustomer,  surname,   name,     id_dir,    address
3,           "Jones",   "Mark",   1,         "some address"
2,           "Davies",  "Sarah",  2,         "other address"
1,           "Willis",  "John",   2,         "other address"
4,           "Ede",     "Linda"   3,         "holliday address"
2,           "Davies",  "Sarah",  4,         "my other house address"
1,           "Willis",  "John",   4,         "my other house address"
4,           "Ede",     "Linda",  5,         "Flat in the beach address"

Until here nothing new. BUT what i'm trying to do is a nested order (don't know how to named) Where order would be the surname related with the address and the customers that owns that address.

Ergo, look first for the first surname (Davies) with the first address, then look for the customer with same address (independently of the surname), and get them (Willis), after that continue with next customer surname or address if there are more from the same customer. And so on.

My desire result:

idcustomer,  surname,   name,   id_dir,    address
2,           "Davies",  "Sarah",  2,       "other address"
1,           "Willis",  "John",   2,       "other address"
2,           "Davies",  "Sarah",  4,       "my other house address"
1,           "Willis",  "John",   4,       "my other house address"
4,           "Ede",     "Linda",  3,       "holliday address"
4,           "Ede",     "Linda",  5,       "Flat in the beach address"
3,           "Jones",   "Mark",   1,       "some address"

Is this even possible using a mysql query?, or a unique/better approach would be to make the example query, save it on an array and order it from there using php? How can i get it in both cases.

I appreciate any help or suggestions!

4

3 回答 3

1

Try order by with desired order: ORDER BY table_one.field_A ASC table_two.field_E DESC

于 2013-07-16T17:53:19.003 回答
1

You would simply order by address first:

ORDER BY id_dir, surname
于 2013-07-16T18:13:39.877 回答
1

you could use mysql's FIELD()

SELECT id_customer, surname, name, id_dir, address
FROM CustomersAddress
INNER JOIN Customers ON Customers.id=CustomersAddress.id_customer
INNER JOIN Address ON Address.id_dir=CustomersAddress.id_address
ORDER BY FIELD(id_dir, 2,4,3,5,1), surname;

SQLFiddle example - http://sqlfiddle.com/#!2/3ea9c/18

this would require either a subquery or a prequery to get the order.

于 2013-07-16T19:01:05.750 回答