为什么学说使用 WHERE IN 而不是 LIMIT?

发布于 2024-10-11 09:22:29 字数 564 浏览 3 评论 0原文

为什么学说(1.2)使用WHERE IN而不是LIMIT

此代码:

Doctrine_Query::create()
   ->from('Table t')
   ->limit(10)
   ->getSqlQuery();

返回类似以下内容:

SELECT t.id_table AS t__id_table FROM table AS t WHERE t__id_table IN (1,2,3,4,10,12,18,20,21,25);

而不是:

SELECT t.id_table AS t__id_table FROM table AS t LIMIT 10;

此行为对于任何 LIMIT 值都是相同的。这会针对高 LIMIT 值生成非常长的查询。

附加问题:Doctrine 如何知道要使用什么 id? (通过向数据库发送另一个查询??)

Why does doctrine (1.2) use WHERE IN instead of LIMIT?

This code:

Doctrine_Query::create()
   ->from('Table t')
   ->limit(10)
   ->getSqlQuery();

Returns something like this:

SELECT t.id_table AS t__id_table FROM table AS t WHERE t__id_table IN (1,2,3,4,10,12,18,20,21,25);

Instead of this:

SELECT t.id_table AS t__id_table FROM table AS t LIMIT 10;

This behaivor is same for any LIMIT value. This generates a very long queries for high LIMIT values.

Bonus question: How does Doctrine know, what ids to use? (By sending another query to DB??)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

回首观望 2024-10-18 09:22:29

这是因为 LIMIT 对数据库行而不是“对象”进行操作。当您输入 $q->limit(10) 时,您希望获取十个对象,而不是数据库中的十行。

考虑以下查询(产品和类别具有多对多关系):

SELECT p.*, c.* FROM product p 
INNER JOIN product_category_ref pcr ON p.id = pcr.prodcut_id
INNER JOIN category c ON c.id = pcr.category_id
WHERE p.price < 123;

要获取 10 个产品(对象),您的查询必须获取至少 20 行。您不能使用LIMIT 10,因为(仅作为示例)只会退回 3 件产品。这就是为什么您需要找出应该获取哪些产品(限制适用于产品),然后获取实际数据。

这将导致以下查询:

SELECT p.id FROM product p WHERE p.price < 123;
SELECT ..... WHERE p.id IN (...);

第二个查询可能返回 20、423 或 31 行。正如您所看到的,这不是来自 limit() 的值。

附言。在这种情况下,Doctrine2 更加清晰,因为它使用 setMaxResults() 方法而不是 limit() ,这样更不容易混淆。

That's because LIMIT operates on database rows not "objects". When you type $q->limit(10) you want to get ten objects, not ten rows from database.

Consider following query (products and categories have many-to-many relationship):

SELECT p.*, c.* FROM product p 
INNER JOIN product_category_ref pcr ON p.id = pcr.prodcut_id
INNER JOIN category c ON c.id = pcr.category_id
WHERE p.price < 123;

To fetch 10 products (objects) your query will have to fetch at least 20 rows. You cannot use LIMIT 10 cause (just for example) only 3 products would be returned. That's why you need to find out which products should be fetched (limit applies to products), and later fetch the actual data.

That will result in following queries:

SELECT p.id FROM product p WHERE p.price < 123;
SELECT ..... WHERE p.id IN (...);

Second query might return 20, 423 or 31 rows. As you can see that's not a value from limit().

PS. Doctrine2 is much more clearer in that case as it's using setMaxResults() method instead of limit() which is less confusing.

乖乖 2024-10-18 09:22:29

使用 Doctrine 1.2.3:

<?php

include(dirname(__FILE__).'/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));

$dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'testdb', 'testdb');
$conn = Doctrine_Manager::connection($dbh);

class Table extends Doctrine_Record {
  public function setTableDefinition() {
    $this->hasColumn('id_table', integer, 10, array('primary' => true));
  }
}

$q = Doctrine_Query::create()
   ->from('Table t')
   ->limit(10)
   ->getSqlQuery();

echo $q;

我得到的结果是:

SELECT t.id_table AS t__id_table FROM table t LIMIT 10

你的代码中是否还有其他事情发生?

Using Doctrine 1.2.3:

<?php

include(dirname(__FILE__).'/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));

$dbh = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'testdb', 'testdb');
$conn = Doctrine_Manager::connection($dbh);

class Table extends Doctrine_Record {
  public function setTableDefinition() {
    $this->hasColumn('id_table', integer, 10, array('primary' => true));
  }
}

$q = Doctrine_Query::create()
   ->from('Table t')
   ->limit(10)
   ->getSqlQuery();

echo $q;

I get the result:

SELECT t.id_table AS t__id_table FROM table t LIMIT 10

Is there maybe something else going on in your code?

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