I'm in big trouble in codeigniter.
I want to use FIND_IN_SET mysql function in codeigniter join() function. But problem is codeigniter consider FIND_IN_SET as a field name.
Please check below code:
$this->db->select("gcpo.promotional_offer_id,gcpo.promotional_offer_name,gcpo.promotional_offer_code,gcpo.promotional_offer_type,gcpo.promotional_offer_discount,gcpo.promotional_offer_min_amount,gcpo.promotional_offer_uses_per_offer,gcpo.promotional_offer_start_date,gcpo.promotional_offer_end_date,name,gcpo.promotional_offer_is_active,gcpo.promotional_offer_added_date,count(gcopo.promotional_offer_code) as cntP");
$this->db->from("promotional_offer gcpo");
$this->db->join("customer_groups", "FIND_IN_SET(id,promotional_offer_customer_group) > 0");
$this->db->join("order_promotional_offer gcopo", "gcopo.promotional_offer_code=gcpo.promotional_offer_code","left");
$this->db->group_by('gcpo.promotional_offer_code');
$this->db->limit($_GET['iDisplayLength'], $start);
$this->db->order_by($sort_array[$_GET['iSortCol_0']], $_GET['sSortDir_0']);
$query = $this->db->get();
In mysql query output which given by codeigniter:
SELECT `gcpo`.`promotional_offer_id`, `gcpo`.`promotional_offer_name`, `gcpo`.`promotional_offer_code`, `gcpo`.`promotional_offer_type`, `gcpo`.`promotional_offer_discount`, `gcpo`.`promotional_offer_min_amount`, `gcpo`.`promotional_offer_uses_per_offer`, `gcpo`.`promotional_offer_start_date`, `gcpo`.`promotional_offer_end_date`, `name`, `gcpo`.`promotional_offer_is_active`, `gcpo`.`promotional_offer_added_date`, count(gcopo.promotional_offer_code) as cntP FROM (`gc_promotional_offer` gcpo) JOIN `gc_customer_groups` ON `FIND_IN_SET`(`id,promotional_offer_customer_group)` > 0 LEFT JOIN `gc_order_promotional_offer` gcopo ON `gcopo`.`promotional_offer_code`=`gcpo`.`promotional_offer_code` GROUP BY `gcpo`.`promotional_offer_code` ORDER BY `gcpo`.`promotional_offer_added_date` desc LIMIT 10
now please find the find_in_set function in mysql query you will find like field name that consider by codeigniter .
FIND_IN_SET is a restriction function, you want to use it with a where. First you need to join customer_groups, and then restrict results with a where.
Change __PUT JOIN CONDITION HERE__ with your condition, and prefix FIND_IN_SET with correct table alias.
$this->db->select("gcpo.promotional_offer_id,gcpo.promotional_offer_name,gcpo.promotional_offer_code,gcpo.promotional_offer_type,gcpo.promotional_offer_discount,gcpo.promotional_offer_min_amount,gcpo.promotional_offer_uses_per_offer,gcpo.promotional_offer_start_date,gcpo.promotional_offer_end_date,name,gcpo.promotional_offer_is_active,gcpo.promotional_offer_added_date,count(gcopo.promotional_offer_code) as cntP");
$this->db->from("promotional_offer gcpo");
$this->db->join("order_promotional_offer gcopo", "gcopo.promotional_offer_code=gcpo.promotional_offer_code","left");
$this->db->join("customer_groups", "__PUT JOIN CONDITION HERE__");
$this->db->where("FIND_IN_SET(id,promotional_offer_customer_group) > 0");
$this->db->group_by('gcpo.promotional_offer_code');
$this->db->limit($_GET['iDisplayLength'], $start);
$this->db->order_by($sort_array[$_GET['iSortCol_0']], $_GET['sSortDir_0']);
$query = $this->db->get();
Related
The following is my mysql query:
select * from db_posts LEFT JOIN db_followers ON db_posts_user_id = db_followers_following AND db_followers_user_id = 276
How can I convert it to codeigniter using query builder?
$this->db->where('db_posts_user_id', 'db_followers_following');
$this->db->where('db_followers_user_id', 276);
$this->db->order_by('db_posts.db_posts_id', 'DESC');
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id');
$query2 = $this->db->get('db_posts')->result_array();
I get the required result when I use mysql query. But I get a blank array when I use codeigniter queries. What is wrong in my CI Query?
why are you using those where clause? if you are using it as condition for your 'JOIN' process, try it like this:
$this->db->order_by('db_posts.db_posts_id', 'DESC');
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id and db_followers_user_id = 276', 'left');
$query2 = $this->db->get('db_posts')->result_array();
Points to Note
1)as previous comments said, you should 'Left Join' if you want to get all posts from 'db_posts'.
2) You can add Multiple conditions in ON Clause using and but within ''. all your conditions should be specified before second comma(,) which is before mentioning 'LEFT'.
Hope this will help. Lemme know if this help
Try adding the "Left" option to tell it what kind of join to do:
$this->db->join('db_followers', 'db_followers.db_followers_following = db_posts.db_posts_user_id', 'Left');
Also I'm not sure you need
$this->db->where('db_posts_user_id', 'db_followers_following');
this is already covered by your JOIN statement.
Source:
http://www.bsourcecode.com/codeigniter/codeigniter-join-query/#left-join and https://www.codeigniter.com/userguide3/database/query_builder.html
How to change the following query with the help of query builder codeigniter?
$query=$this->db->query("select a.*,b.nama from transaksi a,
anggota b
where a.no_transaksi='$nomor' and a.no_transaksi
not in(select no_transaksi from pengembalian)
and a.nomor_anggota=b.nomor_anggota");
Note: just want to know another way
Try this:
$this->db->select('a.*, b.nama');
$this->db->from('transaksi a, anggota b');
$this->db->where('a.no_transaksi', $nomor);
$this->db->where('`a.no_transaksi` NOT IN (SELECT `no_transaksi` FROM `pengembalian`)', NULL, FALSE);
$this->db->where('a.nomor_anggota = b.nomor_anggota');
$result = $this->db->get()->result();
The ,NULL,FALSE in the where() tells CodeIgniter not to escape the query, which may mess it up.
Source: subquery in codeigniter active record
In my controller :
I have function like this :
$this->admindata->examview($a,3);
In model, I just have function like this :
function examview($examid, $examtipe){
$this->db->select("exam_id");
$this->db->from("mainexam");
$query = $this->db->get()
return $query->result();
}
And i got error :
Column 'id_group' in field list is ambiguous
SELECT `mu`.`obli`, `mu`.`id_exam_question`, `p`.`id_question`, `question`, `type_question`, `m`.`id_gabungan`, `p_parent`, `id_group` FROM (`exam`, `exam` mu) LEFT JOIN `randomexam` c ON `mu`.`id_group`= `c`.`id_question_order` LEFT JOIN `question` p ON `p`.`id_question` = `c`.`id_question` LEFT JOIN `main` m ON `m`.`id_question` = `p`.`id_question` WHERE `mu`.`id_exam` = '10' GROUP BY `mu`.`id_exam_question` ORDER BY `question_type` asc, LIMIT 0
I don't even have JOIN in my function. And If I delete $this->admindata->examview($a,3), My error has gone. Codeigniter try to call other function I think.
Nah it's tripping up on something else, a query before this one is in need of fixing. Your query here would be like
SELECT exam_id FROM mainexam;
PHP & MYSQL: How to resolve ambiguous column names in JOIN operation?
Try to track down the query causing the error and apply bandages.
can you help I have two tables images and note. I would like to get all items from these tables and order by date. It is possible I am using active record in codeigniter. Tables are independent.
Thank you for replies.
this should work:
$this->db->orderby("date", "ASC"); //or desc
$query = $this->db->get('images'); //or $query = $this->db->get('note');
$result=$query->result_array();
print_r($result);
or if you want use union all
$this->db->query('SELECT * FROM (SELECT id, date FROM images UNION ALL SELECT id, date FROM note) result ORDER BY result.date');
Notice that each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order from http://www.w3schools.com/sql/sql_union.asp.
I think you want a Cross Join.
Here is your codeigniter code
$this->db->from("images ,note");
$this->db->select("images.*,note.*);
//make sure both dont have same column name other wise use this
//$this->db->select("images.column1,images.column2,note.column1);
$this->db->orderby("images.date", "DESC");
//you can add more orderby
//you can add where condition too
$result=$this->db->get()->result_array();
Now you will get the cross product.
Hope it will help you.
In which table do you have date column? I assume you have it in images table.
$this->db->select('images.coulmn1, images.culmn2, images.date_col, note.col1, note.col2');
$this->db->from('images');
$this->db->join('note', 'note.key1 = images.key1); //key1 is key field to relate both table.
$this->db->order_by("images.date_col", "desc");
$result = $this->db->get()->result_array();
Hope it will help.
Try this:
$this->db->select('*');
$this->db->from('images');
$this->db->join('note');
$this->db->orderby("date column name", "ASC");
$query = $this->db->get();
$result=$query->result_array();
print_r($result);
This code is working perfectaly in mysql run command
SELECT employeeCode
FROM employee_details
WHERE employeeCode
IN (
SELECT DISTINCT (employeeCode) FROM quiz_answer_detailsWHERE submitTime
IN (SELECT MIN( submitTime ) FROM quiz_answer_details WHERE quizId
IN (SELECT id FROM quiz_details WHERE uploadtime = '2014-04-03')
AND answer IN (SELECT answer FROM quiz_details WHERE uploadtime = '2014-04-03'))
)
But I want to use this code on my codeigniter, but it is not working.
My codeigniter query code is
$this->db->select('employeeCode');
$this->db->from('employee_details');
$this->db->where_in('employeeCode');
$this->db->select('DISTINCT(employeeCode)');
$this->db->from('quiz_answer_details');
$this->db->where_in('submitTime');
$this->db->select('min(submitTime)');
$this->db->from('quiz_answer_details');
$this->db->where_in('quizId');
$this->db->select('id');
$this->db->from('quiz_details');
$this->db->where('uploadtime',"2014-04-03");
$this->db->where_in('answer');
$this->db->select('answer');
$this->db->from('quiz_details');
$this->db->where('uploadtime',"2014-04-03");
$query=$this->db->get();
print_r($query);
if($query->num_rows>=1)
{
return $query;
}
else
{
return false;
}
What is wrong please help me
The problem lies with this code and subsequent similar uses of where_in
$this->db->where_in('employeeCode');
You have given the where parameter value but not what to match with.
for eg.
$this->db->where_in('employeeCode',$subQuery1);
The documentation of where_in:
$this->db->where_in();
Generates a WHERE field IN ('item', 'item') SQL query joined with AND
if appropriate
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names); // Produces: WHERE username
IN ('Frank', 'Todd', 'James')
You have to create a separate sub query for each invocation of where_in.
You should re write you subquery and use joins instead to get the better performance,without having full information regarding your tables/relationship and desired result i can't provide you the new query but you can use your subquery in active record's where function
$subquery=" SELECT DISTINCT (employeeCode) FROM quiz_answer_detailsWHERE submitTime
IN (SELECT MIN( submitTime ) FROM quiz_answer_details WHERE quizId
IN (SELECT id FROM quiz_details WHERE uploadtime = '2014-04-03')
AND answer IN (SELECT answer FROM quiz_details WHERE uploadtime = '2014-04-03')) ";
$this->db->select('employeeCode');
$this->db->from('employee_details');
$this->db->where('employeeCode IN('.$subquery.')',null,FALSE);
$query=$this->db->get();
You should pass third parameter as FASLE in order to prevent the query to be quoted by bacticks
Or you can use query() fucntion to run your raw queries
$query=$this->db->query(' your full query here');
$query->result();