使用 CodeIgniter 的查询生成器方法选择查询 WHERE NOT IN 子查询
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
->where()
支持向其传递任何字符串,它将在查询中使用它。你可以尝试使用这个:
where()
中的,NULL,FALSE
告诉 CodeIgniter 不要转义查询,这可能会弄乱它。更新:您还可以查看我编写的子查询库。
->where()
support passing any string to it and it will use it in the query.You can try using this:
The
,NULL,FALSE
in thewhere()
tells CodeIgniter not to escape the query, which may mess it up.UPDATE: You can also check out the subquery library I wrote.
函数
_compile_select()
和_reset_select()
已弃用。而是使用
get_compiled_select()
:The functions
_compile_select()
and_reset_select()
are deprecated.Instead use
get_compiled_select()
:CodeIgniter Active Records 目前不支持子查询,但是我使用以下方法:
_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:
_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.
对于最初的问题来说可能有点晚了,但对于未来的查询这可能会有所帮助。
实现这一目标的最佳方法是
将内部查询的结果获取到这样的数组
然后在下面的活动记录子句中使用比数组
希望这有帮助
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
And then use than array in the following active record clause
Hope this helps
像这样简单的方式。
Like this in simple way .
目前还没有一个答案可以显示最现代、最精致的查询生成器脚本。
where_not_in()
中并关闭转义(默认转义行为会破坏查询)。->select('*')
——这是默认行为。模型方法:
渲染 SQL:
对于任何不想使用子查询的人,可以使用 LEFT JOIN。
渲染的 SQL:
There isn't yet a single answer that shows the most modern and most refined query builder script.
where_not_in()
in the parent query and turn off escaping (the default escaping behavior will ruin the query).->select('*')
-- that is the default behaviour.Model Method:
Rendered SQL:
For anyone who doesn't want to use a subquery, a LEFT JOIN can be used.
Rendered SQL:
对于查询:
SELECT * FROM (SELECT id, Product FROM Product) as Product
,您可以使用:请注意,在 sub_query_from 字符串中,您必须在
...product ) as.. 之间使用空格。 .
For query:
SELECT * FROM (SELECT id, product FROM product) as product
you can use:Please notice, that in sub_query_from string you must use spaces between
... product ) as...
我认为这段代码会起作用。我不知道这在 CI 中是否可以接受的查询风格,但它在我之前的问题中完美地工作。 :)
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. :)
来源:http://www. 247techblog.com/use-write-sub-queries-codeigniter-active-records-condition-full-explaination/
Source : http://www.247techblog.com/use-write-sub-queries-codeigniter-active-records-condition-full-explaination/