使用 CodeIgniter 的查询生成器方法选择查询 WHERE NOT IN 子查询

发布于 2024-11-07 21:27:15 字数 135 浏览 0 评论 0原文

SELECT *
FROM certs
WHERE id NOT IN (SELECT id_cer FROM revokace);

如何在 CodeIgniter 活动记录中编写上述 select 语句?

SELECT *
FROM certs
WHERE id NOT IN (SELECT id_cer FROM revokace);

How do I write the above select statement in CodeIgniter active record?

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

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

发布评论

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

评论(9

離人涙 2024-11-14 21:27:15

->where() 支持向其传递任何字符串,它将在查询中使用它。

你可以尝试使用这个:

$this->db->select('*')->from('certs');
$this->db->where('`id` NOT IN (SELECT `id_cer` FROM `revokace`)', NULL, FALSE);

where() 中的 ,NULL,FALSE 告诉 CodeIgniter 不要转义查询,这可能会弄乱它。

更新:您还可以查看我编写的子查询库

$this->db->select('*')->from('certs');
$sub = $this->subquery->start_subquery('where_in');
$sub->select('id_cer')->from('revokace');
$this->subquery->end_subquery('id', FALSE);

->where() support passing any string to it and it will use it in the query.

You can try using this:

$this->db->select('*')->from('certs');
$this->db->where('`id` NOT IN (SELECT `id_cer` FROM `revokace`)', NULL, FALSE);

The ,NULL,FALSE in the where() tells CodeIgniter not to escape the query, which may mess it up.

UPDATE: You can also check out the subquery library I wrote.

$this->db->select('*')->from('certs');
$sub = $this->subquery->start_subquery('where_in');
$sub->select('id_cer')->from('revokace');
$this->subquery->end_subquery('id', FALSE);
無心 2024-11-14 21:27:15

函数 _compile_select()_reset_select() 已弃用。
而是使用 get_compiled_select()

#Create where clause
$this->db->select('id_cer');
$this->db->from('revokace');
$where_clause = $this->db->get_compiled_select();

#Create main query
$this->db->select('*');
$this->db->from('certs');
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);

The functions _compile_select() and _reset_select() are deprecated.
Instead use get_compiled_select():

#Create where clause
$this->db->select('id_cer');
$this->db->from('revokace');
$where_clause = $this->db->get_compiled_select();

#Create main query
$this->db->select('*');
$this->db->from('certs');
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);
冧九 2024-11-14 21:27:15

CodeIgniter Active Records 目前不支持子查询,但是我使用以下方法:

#Create where clause
$this->db->select('id_cer');
$this->db->from('revokace');
$where_clause = $this->db->_compile_select();
$this->db->_reset_select();

#Create main query
$this->db->select('*');
$this->db->from('certs');
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);

_compile_select() 和 _reset_select() 是两个未记录的(AFAIK)方法,它们编译查询并返回 sql(不运行它)并重置查询。

在主查询中,where 子句中的 FALSE 告诉 codeigniter 不要转义查询(或添加反引号等),这会弄乱查询。 (NULL 只是因为 where 子句有一个可选的第二个参数,我们没有使用)

但是您应该注意,由于 _compile_select() 和 _reset_select() 不是记录方法,因此功能(或存在)将来可能会发生变化发布。

CodeIgniter Active Records do not currently support sub-queries, However I use the following approach:

#Create where clause
$this->db->select('id_cer');
$this->db->from('revokace');
$where_clause = $this->db->_compile_select();
$this->db->_reset_select();

#Create main query
$this->db->select('*');
$this->db->from('certs');
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);

_compile_select() and _reset_select() are two undocumented (AFAIK) methods which compile the query and return the sql (without running it) and reset the query.

On the main query the FALSE in the where clause tells codeigniter not to escape the query (or add backticks etc) which would mess up the query. (The NULL is simply because the where clause has an optional second parameter we are not using)

However you should be aware as _compile_select() and _reset_select() are not documented methods it is possible that there functionality (or existence) could change in future releases.

淡淡绿茶香 2024-11-14 21:27:15

对于最初的问题来说可能有点晚了,但对于未来的查询这可能会有所帮助。
实现这一目标的最佳方法是
将内部查询的结果获取到这样的数组

$this->db->select('id');
$result = $this->db->get('your_table');
return  $result->result_array();

然后在下面的活动记录子句中使用比数组

$this->db->where_not_in('id_of_another_table', 'previously_returned_array');

希望这有帮助

It may be a little late for the original question but for future queries this might help.
Best way to achieve this is
Get the result of the inner query to an array like this

$this->db->select('id');
$result = $this->db->get('your_table');
return  $result->result_array();

And then use than array in the following active record clause

$this->db->where_not_in('id_of_another_table', 'previously_returned_array');

Hope this helps

瑕疵 2024-11-14 21:27:15

像这样简单的方式。

    $this->db->select('*');
    $this->db->from('certs');
    $this->db->where('certs.id NOT IN (SELECT id_cer FROM revokace)');

    return $this->db->get()->result();

Like this in simple way .

    $this->db->select('*');
    $this->db->from('certs');
    $this->db->where('certs.id NOT IN (SELECT id_cer FROM revokace)');

    return $this->db->get()->result();
凌乱心跳 2024-11-14 21:27:15

目前还没有一个答案可以显示最现代、最精致的查询生成器脚本。

  • 使用 get_compiled_select() 声明一个安全子查询。
  • 将子查询传递到父查询中的 where_not_in() 中并关闭转义(默认转义行为会破坏查询)。
  • 选择所有列时,不需要显式链接 ->select('*') ——这是默认行为。

模型方法:

public function getNonRevokedCertificates(): array
{
    $sub = $this->db->select('id_cer')->get_compiled_select('revokace');
    return $this->db->where_not_in('id', $sub, false)->get('certs')->result();
}

渲染 SQL:

SELECT *
FROM `certs`
WHERE id NOT IN(SELECT `id_cer`
FROM `revokace`)

对于任何不想使用子查询的人,可以使用 LEFT JOIN。

public function getNonRevokedCertificates(): array
{
    return $this->db
        ->select('certs.*')
        ->join('revokace', 'revokace.id_cer = certs.id', 'LEFT')
        ->get_where('certs', 'revokace.id_cer IS NULL')
        ->result();
}

渲染的 SQL:

SELECT `certs`.*
FROM `certs`
LEFT JOIN `revokace` ON `revokace`.`id_cer` = `certs`.`id`
WHERE `revokace`.`id_cer` IS NULL

There isn't yet a single answer that shows the most modern and most refined query builder script.

  • Declare a secure subquery with `get_compiled_select().
  • Pass the subquery into where_not_in() in the parent query and turn off escaping (the default escaping behavior will ruin the query).
  • When selecting all columns, there is never any need to explicitly chain ->select('*') -- that is the default behaviour.

Model Method:

public function getNonRevokedCertificates(): array
{
    $sub = $this->db->select('id_cer')->get_compiled_select('revokace');
    return $this->db->where_not_in('id', $sub, false)->get('certs')->result();
}

Rendered SQL:

SELECT *
FROM `certs`
WHERE id NOT IN(SELECT `id_cer`
FROM `revokace`)

For anyone who doesn't want to use a subquery, a LEFT JOIN can be used.

public function getNonRevokedCertificates(): array
{
    return $this->db
        ->select('certs.*')
        ->join('revokace', 'revokace.id_cer = certs.id', 'LEFT')
        ->get_where('certs', 'revokace.id_cer IS NULL')
        ->result();
}

Rendered SQL:

SELECT `certs`.*
FROM `certs`
LEFT JOIN `revokace` ON `revokace`.`id_cer` = `certs`.`id`
WHERE `revokace`.`id_cer` IS NULL
倚栏听风 2024-11-14 21:27:15

对于查询:SELECT * FROM (SELECT id, Product FROM Product) as Product,您可以使用:

$sub_query_from = '(SELECT id, product FROM product ) as product';
$this->db->select();
$this->db->from($sub_query_from);
$query = $this->db->get()

请注意,在 sub_query_from 字符串中,您必须在 ...product ) as.. 之间使用空格。 .

For query: SELECT * FROM (SELECT id, product FROM product) as product you can use:

$sub_query_from = '(SELECT id, product FROM product ) as product';
$this->db->select();
$this->db->from($sub_query_from);
$query = $this->db->get()

Please notice, that in sub_query_from string you must use spaces between ... product ) as...

木有鱼丸 2024-11-14 21:27:15

我认为这段代码会起作用。我不知道这在 CI 中是否可以接受的查询风格,但它在我之前的问题中完美地工作。 :)

$subquery = 'SELECT id_cer FROM revokace';

$this->db->select('*');
$this->db->where_not_in(id, $subquery);
$this->db->from('certs');
$query = $this->db->get();

I think this code will work. I dont know if this is acceptable query style in CI but it works perfectly in my previous problem. :)

$subquery = 'SELECT id_cer FROM revokace';

$this->db->select('*');
$this->db->where_not_in(id, $subquery);
$this->db->from('certs');
$query = $this->db->get();
吃不饱 2024-11-14 21:27:15
$this->db->where('`id` IN (SELECT `someId` FROM `anotherTable` WHERE `someCondition`='condition')', NULL, FALSE);

来源:http://www. 247techblog.com/use-write-sub-queries-codeigniter-active-records-condition-full-explaination/

$this->db->where('`id` IN (SELECT `someId` FROM `anotherTable` WHERE `someCondition`='condition')', NULL, FALSE);

Source : http://www.247techblog.com/use-write-sub-queries-codeigniter-active-records-condition-full-explaination/

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