如何使用 php 从 MySql 按姓名和姓氏排序?
How to sort by name and surname from MySql using php?
我知道我可以这样做 "SELECT * FROM members WHERE isHere = 0 ORDER BY surname"
从我的数据库中获取不在此处的成员并对它们进行排序。但是,如果他们的姓氏相同,我想按名称对他们进行排序。有关如何执行此操作的任何提示?
只需使用多个 ORDER BY
子句即可使用多个排序选项:
SELECT *
FROM members
WHERE isHere = 0
ORDER BY surname ASC,
name ASC
这将首先按姓氏排序,如果两个或多个用户具有相同的姓氏,您可以再按姓名排序。如果你愿意,你也可以添加三分法来按年龄排序。
SELECT *
FROM members
WHERE isHere = 0
ORDER BY surname ASC,
name ASC,
age DESC
您可以对多列进行排序。像这样,
SELECT *
FROM members
WHERE isHere = 0
ORDER BY surname ASC, name ASC;
You can sort on multiple columns, and you can sort different columns
in different directions, which means you can sort the result-set by ASC where as name by DESC.
Which is not required in your case but just for info.
默认排序将不区分大小写。如果要对列进行区分大小写的排序,请为列添加 BINARY 关键字。
像这样,
SELECT *
FROM members
WHERE isHere = 0
ORDER BY BINARY surname ASC, name ASC;
如果你想先名后姓,可以这样使用
SELECT * FROM members WHERE isHere = 0 ORDER BY name asc,surname asc;
如果你想先姓再写名字,可以这样使用
SELECT * FROM members WHERE isHere = 0 ORDER BY surname asc,name asc;
根据
排序方式更改asc
和desc
我知道我可以这样做 "SELECT * FROM members WHERE isHere = 0 ORDER BY surname"
从我的数据库中获取不在此处的成员并对它们进行排序。但是,如果他们的姓氏相同,我想按名称对他们进行排序。有关如何执行此操作的任何提示?
只需使用多个 ORDER BY
子句即可使用多个排序选项:
SELECT *
FROM members
WHERE isHere = 0
ORDER BY surname ASC,
name ASC
这将首先按姓氏排序,如果两个或多个用户具有相同的姓氏,您可以再按姓名排序。如果你愿意,你也可以添加三分法来按年龄排序。
SELECT *
FROM members
WHERE isHere = 0
ORDER BY surname ASC,
name ASC,
age DESC
您可以对多列进行排序。像这样,
SELECT *
FROM members
WHERE isHere = 0
ORDER BY surname ASC, name ASC;
You can sort on multiple columns, and you can sort different columns in different directions, which means you can sort the result-set by ASC where as name by DESC.
Which is not required in your case but just for info.
默认排序将不区分大小写。如果要对列进行区分大小写的排序,请为列添加 BINARY 关键字。
像这样,
SELECT *
FROM members
WHERE isHere = 0
ORDER BY BINARY surname ASC, name ASC;
如果你想先名后姓,可以这样使用
SELECT * FROM members WHERE isHere = 0 ORDER BY name asc,surname asc;
如果你想先姓再写名字,可以这样使用
SELECT * FROM members WHERE isHere = 0 ORDER BY surname asc,name asc;
根据
排序方式更改asc
和desc