Home  >  Article  >  Backend Development  >  How to Exclude Records Using Subqueries with CodeIgniter\'s Query Builder?

How to Exclude Records Using Subqueries with CodeIgniter\'s Query Builder?

Barbara Streisand
Barbara StreisandOriginal
2024-11-18 07:31:02339browse

How to Exclude Records Using Subqueries with CodeIgniter's Query Builder?

Selecting Records Using CodeIgniter's Query Builder with Subquery Exclusion

CodeIgniter's query builder provides various methods for constructing SQL queries. One common scenario involves excluding records based on a subquery. To accomplish this, the where() method can be employed, as demonstrated below:

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

The NULL, FALSE parameters within the where() method instruct CodeIgniter to refrain from escaping the query, preserving its intended format.

Alternatively, the subquery library can be utilized for greater flexibility:

->select('*')
->from('certs')
->subquery->start_subquery('where_in')
->subquery->select('id_cer')
->subquery->from('revokace')
->subquery->end_subquery('id', FALSE);

This method provides a convenient mechanism for constructing subqueries within the framework of CodeIgniter's query builder, allowing for more complex and dynamic data retrieval operations.

The above is the detailed content of How to Exclude Records Using Subqueries with CodeIgniter\'s Query Builder?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn