sql查询顺序似乎不起作用

时间:2022-10-13 12:16:46

I have this table structure,

我有这个表结构,

| id | name | level |
---------------------
| 1  |  a   |   1   |
| 2  |  b   |   2   |
| 3  |  c   |   3   |
| 5  |  d   |   4   |
| 6  |  e   |   1   |
| 7  |  f   |   2   |
| 8  |  g   |   1   |
| 9  |  g   |   4   |

I want to order my fetch result to level, so I execute this query:

我想将我的获取结果命令为level,因此我执行此查询:

$sql = "SELECT * FROM section_tb WHERE id = ? ORDER BY level";
$stmt = $db->prepare($sql);
$stmt->execute(array($id));
$result = $stmt->fetch(PDO::FETCH_ASSOC);

However when I print_r($result) the order seems like it was sorted by id. I am confused why.

但是,当我print_r($ result)时,订单似乎按ID排序。我很困惑为什么。

My db details:

我的数据库细节:

id - PRIMARY, AUTO INCREMENT
name - UNIQUE
INNODB

3 个解决方案

#1


2  

Your query is ordering correctly.

您的查询正确排序。

This is irrelevant since it's only returning one row each time its called anyway.

这是无关紧要的,因为它每次调用它时只返回一行。

Your foreach is calling into it multiple times, and the ordering only affects the actual database call. Therefore the overall order of the results is the order of that foreach.

您的foreach多次调用它,并且排序仅影响实际的数据库调用。因此,结果的整体顺序是该foreach的顺序。

If the foreach had passed a parameter that identified more than one row, then within each of those calls the order would be by level (e.g. if you'd done queries to match on name, then the two that match "g" would be in the order requested).

如果foreach已经传递了一个标识多个行的参数,那么在每个调用中,顺序将按级别进行(例如,如果您完成了与名称匹配的查询,那么匹配“g”的两个将在要求的订单)。

You want to change the query to something like SELECT * FROM section_tb WHERE id in (1,2,3,4,5,6,7,8,9) ORDER BY level (or perhaps just SELECT * FROM section_tb ORDER BY level), call it once, and loop through the results.

您想要将查询更改为SELECT * FROM section_tb WHERE id in(1,2,3,4,5,6,7,8,9)ORDER BY level(或者可能只是SELECT * FROM section_tb ORDER BY级别) ,调用一次,并循环结果。

#2


1  

Your WHERE clause is seeking an id which you've identified as the primary key, so your query should only return one row.

您的WHERE子句正在寻找您已识别为主键的ID,因此您的查询应仅返回一行。

#3


0  

You can't do that. You can either use a place holder or bind the parameter.

你不能这样做。您可以使用占位符或绑定参数。

#1


2  

Your query is ordering correctly.

您的查询正确排序。

This is irrelevant since it's only returning one row each time its called anyway.

这是无关紧要的,因为它每次调用它时只返回一行。

Your foreach is calling into it multiple times, and the ordering only affects the actual database call. Therefore the overall order of the results is the order of that foreach.

您的foreach多次调用它,并且排序仅影响实际的数据库调用。因此,结果的整体顺序是该foreach的顺序。

If the foreach had passed a parameter that identified more than one row, then within each of those calls the order would be by level (e.g. if you'd done queries to match on name, then the two that match "g" would be in the order requested).

如果foreach已经传递了一个标识多个行的参数,那么在每个调用中,顺序将按级别进行(例如,如果您完成了与名称匹配的查询,那么匹配“g”的两个将在要求的订单)。

You want to change the query to something like SELECT * FROM section_tb WHERE id in (1,2,3,4,5,6,7,8,9) ORDER BY level (or perhaps just SELECT * FROM section_tb ORDER BY level), call it once, and loop through the results.

您想要将查询更改为SELECT * FROM section_tb WHERE id in(1,2,3,4,5,6,7,8,9)ORDER BY level(或者可能只是SELECT * FROM section_tb ORDER BY级别) ,调用一次,并循环结果。

#2


1  

Your WHERE clause is seeking an id which you've identified as the primary key, so your query should only return one row.

您的WHERE子句正在寻找您已识别为主键的ID,因此您的查询应仅返回一行。

#3


0  

You can't do that. You can either use a place holder or bind the parameter.

你不能这样做。您可以使用占位符或绑定参数。