SELECT sum(`quantity`) as 'Total Product Sales',product.i_name as 'Product Name'
FROM `order_details`
inner join orders on orders.order_id = order_details.order_id and orders.status like 'C'
inner join product on product.p_id = order_details.product_id
group by order_details.product_id
You can try this Codeigniter Active Query :
Query :
public function get_data(){
$this->db->select("sum(order_details.quantity) as total_product_sales", FALSE);
$this->db->select("product.i_name as product_name'", FALSE);
$this->db->from('order_details');
$this->db->join('orders', 'orders.order_id = order_details.order_id','INNER');
$this->db->join('product', 'product.p_id = order_details.product_id', 'INNER');
$this->db->like('orders.status', 'C');
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
I hope it will Help.
Related
I have a raw sql query which i run using yii2 Yii::$app->db->createCommand() but then i wonder i how i can use yii's method of writing queries to actualize the same thing.
$m = "SELECT p.basic_plan_amt AS basic,p.premium_plan_amt AS premium,p.daju_plan_amt AS daju,c.name
AS country FROM price p LEFT JOIN country c ON p.country_id = c.id WHERE p.country_id = " .$country_id;
though the above query works well and provide expected result, but then how can i write the same query using the below format
$model = \backend\models\Price::find()->select([p.basic_plan_amt AS basic,p.premium_plan_amt AS
premium,p.daju_plan_amt AS daju,c.name
AS country])->where(['country_id' => $country_id])->all();
Below are the examples of query in yii hope it will help you More reference
Relation Model
$model = User::find()
->with('comments')
->all();
foreach ($model as $user) {
// get data from relation model
$comments = $user->comments;
......
foreach($comments as $comment){
........
}
}
joinWith()
Sample 1:
$model = User::find()
->joinWith('comments')
->all();
Sample 2:
$model = User::find()
->joinWith('comments')
->orderBy('tbl_comments_id.id, tbl_user.id')
->all();
innerJoinWith()
$model = User::find()
->innerJoinWith('comments', false)
->all();
// equivalent to the above
$model = User::find()
->joinWith('comments', false, 'INNER JOIN')
->all();
Join()
JOIN_TYPE = INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN etc
Syntax
$query = new Query;
$query ->select(['SELECT COLUMNS'])
->from('TABLE_NAME_1')
->join( 'JOIN_TYPE',
'TABLE_NAME_2',
'TABLE_NAME_2.COLUMN =TABLE_NAME_1.COLUMN'
);
$command = $query->createCommand();
$data = $command->queryAll();
Sample 1:
$query = new Query;
$query ->select([
'tbl_user.username AS name',
'tbl_category.categoryname as Category',
'tbl_document.documentname']
)
->from('tbl_user')
->join('LEFT OUTER JOIN', 'tbl_category',
'tbl_category.createdby =tbl_user.userid')
->join('LEFT OUTER JOIN', 'tbl_document',
'tbl_category.cid =tbl_document.did')
->LIMIT(5) ;
$command = $query->createCommand();
$data = $command->queryAll();
Output Query
SELECT `tbl_user`.`username` AS `name`, `tbl_category`.`categoryname` AS `Category`
FROM `tbl_user`
LEFT OUTER JOIN `tbl_category` ON tbl_category.createdby =tbl_user.userid
LEFT OUTER JOIN `tbl_document` ON tbl_category.cid =tbl_document.did
LIMIT 5
leftJoin()
Sample 1:
$query = new Query;
$query ->select(['tbl_user.username AS name', 'tbl_category.type as Category'])
->from('tbl_user')
->leftJoin('tbl_category', 'tbl_category.createdby = tbl_user.userid')
->limit(2);
$command = $query->createCommand();
$data = $command->queryAll();
Output Query
SELECT `tbl_user`.`username` AS `name`, `tbl_category`.`type` AS `Category`
FROM `tbl_user`
LEFT JOIN `tbl_category` ON tbl_category.createdby = tbl_user.useridd
LIMIT 2
use createcommand() method:
use yii\db\Query();
$connection = \Yii::$app->db;
$query = new Query;
$insql = $connection->createCommand("SELECT* FROM inventory );
$result=$insql->queryAll();
the above method list all data from the inventory table.
How to write this query in codeigniter active records
select
a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,
c.sub_child_cat_id,c.sub_child_cat_name
FROM parent_categories a,child_categories b,sub_child_categories c
WHERE a.parent_cat_id=b.parent_cat_id AND b.child_cat_id=c.child_cat_id
Tried this but it Shows 0 result
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->where('a.parent_cat_id','b.parent_cat_id');
$this->db->where('b.child_cat_id','c.child_cat_id');
$result = $this->db->get()->result_array();
when i echo the above ci query i get
SELECT `a`.`parent_cat_id`, `a`.`parent_cat_name`, `b`.`child_cat_id`, `b`.`child_cat_name`, `c`.`sub_child_cat_id`, `c`.`sub_child_cat_name`
FROM `parent_categories` `a`, `child_categories` `b`, `sub_child_categories` `c`
WHERE `a`.`parent_cat_id` = 'b.parent_cat_id'
AND `b`.`child_cat_id` = 'c.child_cat_id'
Try changing $this->db->where in your query as below-
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->where("a.parent_cat_id = b.parent_cat_id");
$this->db->where("b.child_cat_id = c.child_cat_id");
$result = $this->db->get()->result_array();
You have to use Join Query For That Here is Code Snippet
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a');
$this->db->join('child_categories b', 'b.parent_cat_id = a.parent_cat_id', 'left');
$this->db->join('sub_child_categories c', 'c.child_cat_id = b.child_cat_id', 'left');
$query = $this->db->get();
$res = $query->result();
I didn't have your table structure and data to check. But, try this. It will work.
$this->db->select('a.parent_cat_id,a.parent_cat_name, b.child_cat_id,b.child_cat_name,c.sub_child_cat_id,c.sub_child_cat_name');
$this->db->from('parent_categories a,child_categories b,sub_child_categories c');
$this->db->join('a.parent_cat_id','b.parent_cat_id');
$this->db->join('b.child_cat_id','c.child_cat_id');
$this->db->get();
I am trying to select column name level_name from table levels,
which contains level_id and level_name,
for a user to know what is their level,
The table of users named as users and contain a level_id and user_id,
but I get this error ->
Column 'level_id' in on clause is ambiguous
SELECT `level_name`
FROM `levels`
JOIN `users` ON `level_id` = `level_id` WHERE `user_id` = '9'
here it is the code in the model
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels');
$this->db->join('users', 'level_id = level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
thanks in advance :)
Change the query to
SELECT `level_name`
FROM `levels` l
JOIN `users` u ON `u`.`level_id` = `l`.`level_id`
WHERE `user_id` = '9'
if you like the table name aliasing method, it is shorter and easier to read.
Or
SELECT `level_name`
FROM `levels`
JOIN `users` ON `users`.`level_id` = `levels`.`level_id`
WHERE `user_id` = '9'
If you prefere to use the full table name everywhere.
Because both tables contain a column with the name level_id the query analyser need to know which one you are addressing.
In codeigniter try
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels l');
$this->db->join('users u', 'u.level_id = l.level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
Select l.level_name
FROM levels l
JOIN users u
ON u.level_id = l.level_id
and u.user_id = '9'
public function level_ownprofile($user_id)
{
$this->db->select('level_name');
$this->db->from('levels');
$this->db->join('users', 'levels.level_id = users.level_id');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query;
}
public function level_ownprofile($user_id)
{
$this->db->select('l.level_name');
$this->db->from('levels as l');
$this->db->join('users as u', 'l.level_id = u.level_id');
$this->db->where('l.user_id', $user_id);
$query = $this->db->get();
return $query->results();
}
I'm trying to get an entire row values from a new SQL query so.
How can I make a function
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
to something like this :
In model page
public function getID_researcher($lastname){
$query = $this->db->get_where('researcher', array('lastname' => $lastname));
return $query->row_array();
}
I need to return one row result base on the lastname which is from table1
To clarify here, 'researcher' is the table where it gets data but what I want is the new SQL for me to get the data.
I tried this one but still error.
public function getID_researcher($lastname){
$sql = = $this->db->query('Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id');
$query = $this->db->get_where($sql, array('lastname' => $lastname));
return $query->row_array();
}
to use this query
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
you can try to use active record db 'join' like this:
$this->db->from('table1 t1');
$this->db->join('table2 t2', 't1.t1_id = t2.t1_id', 'left');
$this->db->join('table3 t3', 't3.id = t2.t3_id', 'left');
$this->db->where('lastname', $lastname);
$query = $this->db->get();
$c = 0;
foreach($query->result() as $q){
$result[$c]['name'] = $q->lastname;
// put move variable here
$c++;
}
return $result;
hope this help
I have this query
SELECT
arl_kind,
IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
arl_display_nr
FROM
tof_art_lookup
LEFT JOIN
tof_brands
ON
bra_id = arl_bra_id
INNER JOIN
tof_articles
ON
tof_articles.art_id = tof_art_lookup.arl_art_id
INNER JOIN
tof_suppliers
ON
tof_suppliers.sup_id = tof_articles.art_sup_id
WHERE
arl_art_id = #art_id
AND arl_kind IN (3)
ORDER BY
arl_kind,
bra_brand,
arl_display_nr limit 100;
when I execute this on phpmyadmin or in any software that has sql comand capabilities everything works and the output is as i should.
The problem appears when I try to insert this in codeigniter, and my main probles is with the if statement, I tried to insert it like this
return $query = $this->db
->SELECT('
arl_kind,
IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
arl_display_nr
FROM
tof_art_lookup
LEFT JOIN
tof_brands
ON
bra_id = arl_bra_id
INNER JOIN
tof_articles
ON
tof_articles.art_id = tof_art_lookup.arl_art_id
INNER JOIN
tof_suppliers
ON
tof_suppliers.sup_id = tof_articles.art_sup_id
WHERE
arl_art_id = #art_id
AND arl_kind IN (3)
ORDER BY
arl_kind,
bra_brand,
arl_display_nr limit 100')
->get();
and second option like that
return $query = $this->db
->select('ARL_KIND, IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,ARL_DISPLAY_NR')
->from('tof_art_lookup')
->join('tof_brands','bra_id = arl_bra_id','LEFT')
->join('tof_articles','tof_articles.art_id = tof_art_lookup.arl_art_id','INNER')
->join('tof_suppliers','tof_suppliers.sup_id = tof_articles.art_sup_id','INNER')
->where('ARL_ART_ID',$id)
->where_in('ARL_KIND',array('3'))
//->where('A.ARL_DISPLAY','0')
->group_by('arl_kind','bra_brand','arl_display_nr')
->get();
both of them work but partialy without the second select option the one with the if.
Can anyone help me to fix this.
I believe your code should look like this
return $query = $this->db
->select('arl_kind, IF(tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,ARL_DISPLAY_NR', FALSE)
->from('tof_art_lookup')
->join('tof_brands', 'bra_id = arl_bra_id', 'LEFT')
->join('tof_articles', 'tof_articles.art_id = tof_art_lookup.arl_art_id', 'INNER')
->join('tof_suppliers','tof_suppliers.sup_id = tof_articles.art_sup_id', 'INNER')
->where('arl_art_id', $id)
->where_in('arl_kind', array('3'))
->order_by('arl_kind','bra_brand','arl_display_nr')
->limit(100)
->get();
Note: second parameter of select() method is set to FALSE and order_by() is used instead of group_by() as in your original query.
use like this
$query_string = "SELECT
arl_kind,
IF (tof_art_lookup.arl_kind = 2, tof_suppliers.sup_brand, tof_brands.bra_brand) AS brand,
arl_display_nr
FROM
tof_art_lookup
LEFT JOIN
tof_brands
ON
bra_id = arl_bra_id
INNER JOIN
tof_articles
ON
tof_articles.art_id = tof_art_lookup.arl_art_id
INNER JOIN
tof_suppliers
ON
tof_suppliers.sup_id = tof_articles.art_sup_id
WHERE
arl_art_id = #art_id
AND arl_kind IN (3)
ORDER BY
arl_kind,
bra_brand,
arl_display_nr limit 100";
$query=$this->db->query($query_string);
if ($query->num_rows () > 0) {
return $query->result_array ();
} else {
return FALSE;
}