Codeigniter 4 – Query builder – select, where, where_in, where_not_in, with and or, like, or_like, not_like, having, group by, insert, update, update_batch, delete in codeigniter 4 with examples
SELECT
- Use the select() method to specify the columns to retrieve.
Example:
$db->select('name, email')->from('my_table')->get();
WHERE
- Use the where() method to filter data based on conditions.
Example:
$db->select('name, email')->from('my_table')->where('name', 'John Doe')->get();
WHERE_IN
- Use the where_in() method to filter data based on multiple values.
Example:
$db->select('name, email')->from('my_table')->where_in('name', ['John Doe', 'Jane Doe'])->get();
WHERE_NOT_IN
- Use the where_not_in() method to filter data based on multiple values that are not present.
Example:
$db->select('name, email')->from('my_table')->where_not_in('name', ['John Doe', 'Jane Doe'])->get();
WITH
- Use the with() method to join multiple tables.
Example:
$db->select('my_table.name, another_table.email')->from('my_table')->with('another_table', '(link unavailable) = (link unavailable)')->get();
OR
- Use the or_where() method to filter data based on multiple conditions.
Example:
$db->select('name, email')->from('my_table')->where('name', 'John Doe')->or_where('email', 'john@example.com')->get();
LIKE
- Use the like() method to filter data based on a pattern.
Example:
$db->select('name, email')->from('my_table')->like('name', 'John%')->get();
OR_LIKE
- Use the or_like() method to filter data based on multiple patterns.
Example:
$db->select('name, email')->from('my_table')->like('name', 'John%')->or_like('email', '%@example.com')->get();
NOT_LIKE
- Use the not_like() method to filter data based on a pattern that is not present.
Example:
$db->select('name, email')->from('my_table')->not_like('name', 'John%')->get();
HAVING
- Use the having() method to filter data based on a condition after grouping.
Example:
$db->select('name, email')->from('my_table')->group_by('name')->having('name', 'John Doe')->get();
GROUP BY
- Use the group_by() method to group data based on a column.
Example:
$db->select('name, email')->from('my_table')->group_by('name')->get();
INSERT
- Use the insert() method to insert data into a table.
Example:
$db->insert('my_table', ['name' => 'John Doe', 'email' => 'john@example.com']);
UPDATE
- Use the update() method to update data in a table.
Example:
$db->update('my_table', ['name' => 'Jane Doe'], ['id' => 1]);
UPDATE_BATCH
- Use the update_batch() method to update multiple rows in a table.
Example:
$db->update_batch('my_table', ['name' => 'Jane Doe'], ['id' => 1], ['id' => 2]);
DELETE
- Use the delete() method to delete data from a table.
Example:
$db->delete('my_table', ['id' => 1]);
I hope this helps! Let me know if you have any further questions or need more information.