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!