Riandy
Riandy

Reputation: 23

Make Query in Codeigniter

I have some queries here with native procesdure can anyone help me to convert it to CODEIgniter style that saved in MODEL Folder.

Here my Query :

SELECT
CONCAT(address.firstname,' ',
            address.lastname) AS `Name`,
address.email AS Email,
items.created_at AS Date,
items.`name` AS Description,
items.store_id AS Logon,
items.`name` AS Category,
items.store_id AS FeedbackDate,
items.sku AS ProductSearchcode,
items.order_id AS Orderref,
orders.grand_total
FROM sales_flat_order AS orders
      JOIN sales_flat_order_item AS items
      ON items.order_id = orders.entity_id
      LEFT JOIN sales_flat_order_address AS address
      ON orders.entity_id = address.parent_id
WHERE   
   items .created_at BETWEEN  '2016-07-01' AND '2016-07-31'
  AND orders.status = 'complete'

I hope anyone can help me :) Regards

Upvotes: 2

Views: 371

Answers (4)

Razib Al Mamun
Razib Al Mamun

Reputation: 2713

All answer is correct, but batter is used with Query Builder Class like bellow :

$this->db->select("CONCAT(address.firstname,' ',address.lastname) AS `Name`,
address.email AS Email, items.created_at AS Date, items.`name` AS Description,
items.store_id AS Logon, items.`name` AS Category, items.store_id AS FeedbackDate,
items.sku AS ProductSearchcode, items.order_id AS Orderref, orders.grand_total");

$this->db->from("sales_flat_order AS orders");
$this->db->join("sales_flat_order_item AS items", "items.order_id = orders.entity_id");
$this->db->join("sales_flat_order_address AS address", "orders.entity_id = address.parent_id", "LEFT");
$this->db->where("items.created_at BETWEEN  '2016-07-01' AND '2016-07-31'");
$this->db->where("orders.status", "complete");

$query = $this->db->get();
$result = $query->result_array();

Upvotes: 3

BIBIN JOHN
BIBIN JOHN

Reputation: 354

Join Query- Active Records

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
$result=$query->result_array();

Upvotes: 1

Happy Coding
Happy Coding

Reputation: 2525

You can directly use your query in this way :

$query = "your sql query"; 
$db_query = $this->db->query($query); 
return $db_query->result_array(); 

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

In Codeigniter you can run your custom query using

$this->db->query();

in case if you query is to complex like:

$this->db->query(" your query ");

Upvotes: 1

Related Questions