Home >Backend Development >PHP Tutorial >How can I execute UNION queries using CodeIgniter's ActiveRecord pattern?
Union Queries with CodeIgniter's ActiveRecord Pattern
The ActiveRecord pattern, widely used in CodeIgniter, offers a convenient way to perform database operations. However, it does not natively support UNION queries. There is a workaround, though, that involves writing your own query and utilizing the ActiveRecord's query method.
Query Construction
To execute a UNION query, start by composing your query string as follows:
SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2
CodeIgniter Implementation
Once your query is ready, use CodeIgniter's DB class to execute it:
$this->db->query('SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2');
This query will perform a UNION operation, combining the results from table_name1 and table_name2 into a single set of records.
The above is the detailed content of How can I execute UNION queries using CodeIgniter's ActiveRecord pattern?. For more information, please follow other related articles on the PHP Chinese website!