查询中的位置参数和命名参数是什么意思?

发布于 2024-08-29 13:33:48 字数 253 浏览 4 评论 0原文

这里我们有一个位置参数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相守太难 2024-09-05 13:33:48

位置参数由其在子句中的索引设置。

命名参数由其名称设置。

当您设置值时,您可能会将值存储在数组中,在这种情况下,位置形式可能更有用。或者,您可以按名称将它们放在关联数组中,在这种情况下,命名形式更有用。


更新 - 尽管文档引用了位置参数,例如 ?1,但示例仅使用 ?

此位置参数示例按提供的数组中的位置将值映射到查询中的位置占位符中。

$q = Doctrine_Query::create()
  ->from('User u')
  ->where('u.username = ? and u.age = ?', array('Arnold', 50));

$users = $q->fetchArray();

但是,此示例按关联数组中的名称将值映射到其命名占位符。看看他们如何不需要井井有条。

$q = Doctrine_Query::create()
  ->from('User u')
  ->where('u.username = :username  and u.age = :age',
      array(':age' => 50, ':username' => 'Arnold'));

(必须承认我不是 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.

$q = Doctrine_Query::create()
  ->from('User u')
  ->where('u.username = ? and u.age = ?', array('Arnold', 50));

$users = $q->fetchArray();

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.

$q = Doctrine_Query::create()
  ->from('User u')
  ->where('u.username = :username  and u.age = :age',
      array(':age' => 50, ':username' => 'Arnold'));

(Have to admit I'm not a PHP guy - above based on the examples here.)

终弃我 2024-09-05 13:33:48

位置参数按其在查询中的顺序指定。命名参数由其名称指定。

使用位置参数时,您必须按照查询中使用的顺序添加它们,如果您想多次使用相同的值,则必须将其作为单独的参数多次添加。

使用名称参数时,您可以按所需的任意顺序添加它们,并且一个参数可以在查询中多次使用。

例如,如果您有一个在多个字段中搜索的查询,则使用位置参数,它可能如下所示:

select u.UserId, u.UserName
from FormumUser u
where u.UserName like ? or u.Email like ? or u.Address like ?

您必须将搜索字符串作为单独的参数添加三次。使用名称参数,它可能看起来像:

select u.UserId, u.UserName
from FormumUser u
where u.UserName like @find or u.Email like @find or u.Address like @find

那么您只需添加一个参数,因为查询可以在三个地方使用相同的参数。

(当然,在查询中使用参数的确切语法会有所不同,具体取决于您使用的数据库解决方案。)

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:

select u.UserId, u.UserName
from FormumUser u
where u.UserName like ? or u.Email like ? or u.Address like ?

You would have to add the search string three times as separate parameters. Using names parameters it could look like:

select u.UserId, u.UserName
from FormumUser u
where u.UserName like @find or u.Email like @find or u.Address like @find

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.)

梦幻的味道 2024-09-05 13:33:48

我不知道我是否理解正确,所以这就是我的想法:

位置参数应该使用整数索引进行索引,命名参数应该通过它们的名称来访问。

示例(这是伪代码):

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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文