How to Perform SELECT Queries with Subquery Exclusion Using CodeIgniter\'s Query Builder?

Mary-Kate Olsen
Release: 2024-11-18 22:16:02
Original
773 people have browsed it

How to Perform SELECT Queries with Subquery Exclusion Using CodeIgniter's Query Builder?

Utilizing CodeIgniter's Query Builder for SELECT Queries with Subquery Exclusion

CodeIgniter offers a versatile query builder that facilitates the creation of complex SQL queries in PHP. One such query is the SELECT query with a subquery exclusion. This query allows you to retrieve rows from a table based on a condition that excludes specific rows from another table, as expressed in the following SQL statement:

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

To replicate this query using CodeIgniter's query builder, you can leverage the where() method. This method accepts a string argument that represents the query condition:

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

In this code, the ,NULL,FALSE arguments in the where() method prevent CodeIgniter from escaping the query, ensuring its proper execution.

Alternatively, you can employ the subquery library to simplify the process further:

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

By utilizing CodeIgniter's query builder and its where() or subquery methods, you can effortlessly execute complex SELECT queries with subquery exclusions in your PHP applications.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template