查询中的位置参数和命名参数是什么意思?
这里我们有一个位置参数:
SELECT
u
FROM ForumUser u
WHERE u.id = ?1
这里有一个命名参数:
SELECT
u
FROM ForumUser u
WHERE u.username = :name
这是 DQL(教义查询语言),但我认为概念是相同的。
有人可以解释一下这些是什么意思和作用吗?
here we got a positional parameter:
SELECT
u
FROM ForumUser u
WHERE u.id = ?1
and here a named parameter:
SELECT
u
FROM ForumUser u
WHERE u.username = :name
this is DQL (doctrine query language) but i think the concept is the same.
could someone please explain what these mean and do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
位置参数由其在子句中的索引设置。
命名参数由其名称设置。
当您设置值时,您可能会将值存储在数组中,在这种情况下,位置形式可能更有用。或者,您可以按名称将它们放在关联数组中,在这种情况下,命名形式更有用。
更新 - 尽管文档引用了位置参数,例如
?1
,但示例仅使用?
。此位置参数示例按提供的数组中的位置将值映射到查询中的位置占位符中。
但是,此示例按关联数组中的名称将值映射到其命名占位符。看看他们如何不需要井井有条。
(必须承认我不是 PHP 人员 - 上面基于示例 此处。)
A positional parameter is set by its index in the clause.
A named parameter is set by its name.
When you are setting the values, you might have the values in an array, in which case the positional form could me more useful. Alternatively, you might have them in an associative array by name, in which case the named form is more useful.
Update - Although the documentation refers to positional parameters as for example
?1
, the examples just use?
.This example of positional parameters maps values by position in the array provided into the positional placeholders in the query.
However this example maps values by name in an associative array to their named placeholders. See how they do not need tgo be in order.
(Have to admit I'm not a PHP guy - above based on the examples here.)
位置参数按其在查询中的顺序指定。命名参数由其名称指定。
使用位置参数时,您必须按照查询中使用的顺序添加它们,如果您想多次使用相同的值,则必须将其作为单独的参数多次添加。
使用名称参数时,您可以按所需的任意顺序添加它们,并且一个参数可以在查询中多次使用。
例如,如果您有一个在多个字段中搜索的查询,则使用位置参数,它可能如下所示:
您必须将搜索字符串作为单独的参数添加三次。使用名称参数,它可能看起来像:
那么您只需添加一个参数,因为查询可以在三个地方使用相同的参数。
(当然,在查询中使用参数的确切语法会有所不同,具体取决于您使用的数据库解决方案。)
Positional parameters are specified by their order in the query. Named parameters are specified by their names.
When using positional parameters you have to add them in the same order that they are used in the query, and if you want to use the same value more than once you have to add it multiple times as separate parameters.
When using names parameters you can add them in any order you want, and a parameter can be used more than once in the query.
For example if you have a query that searches in several fields, using positional parameters it could look like this:
You would have to add the search string three times as separate parameters. Using names parameters it could look like:
Then you would only add one parameter, as the query can use the same parameter in three places.
(The exact syntax for using the parameters in the query of course varies depending on what database solution you are using.)
我不知道我是否理解正确,所以这就是我的想法:
位置参数应该使用整数索引进行索引,命名参数应该通过它们的名称来访问。
示例(这是伪代码):
query.SetParameter(0, 456); // 这里我们将值 456 设置为第一个参数,该参数的索引为零
query.SetParameter("用户名", "约翰·史密斯"); // 这里我们将值“John Smith”设置为名为“username”的参数
I don't kown if I Understood right, so this is what I think:
Positional parameters should be indexed using an integer index, and named parameters should be accessed though their names.
Example (this is pseudocode):
query.SetParameter(0, 456); // here we set value 456 to the first parameter, which has index zero
query.SetParameter("username", "John Smith"); // here we set value "John Smith" to the parameter named "username"