there is 4 table
option_group(id, optionGroupName)
category_optiongroup(categoryId, optionGroupId, inOrder)
product_option(productId, optionGroupId, optionId)
option(id, optionValue)
some products has not inserted its optionValue yet in the product_option table but I want to get theme all Whether is inserted or not.
for example size of specific product has not set yet.
here is my model but it only return all optionValue that is set.
is it some how possible to do it with IFNULL()? if not doesn't matter
IFNULL(optionValue, 'Not Set')
$this->db->select('option.optionValue')
->from('category_optiongroup')
->where('categoryId', $data['categoryId'])
->join('product_option', 'product_option.productId='.$productId.
' AND product_option.optionGroupId=category_optiongroup.optionGroupId', 'left')
->join('option', 'option.id=product_option.optionId')
->order_by('category_optiongroup.inOrder', 'ASC');
$query = $this->db->get();
return $query;
two tips:
1st: both option and product_option tables needs left join
2nd: certainly IFNULL() needs an alias to call
$this->db->select('IFNULL(`option`.`optionValue`, "Not Set") AS optionValue', FALSE)
->from('category_optiongroup')
->join('product_option', 'product_option.productId='.$productId
.' AND product_option.optionGroupId=category_optiongroup.optionGroupId', 'left')
->join('option', 'option.id=product_option.optionId', 'left')
->where('categoryId', $data['categoryId'])
->order_by('category_optiongroup.inOrder', 'ASC');
$query = $this->db->get();
return $query;
You can use IFNULL like this
$this->db->select('IFNULL(`option`.optionValue,"Not Set")',false)
->from('category_optiongroup')
->join('product_option', 'product_option.productId='.$productId.
' AND product_option.optionGroupId = category_optiongroup.optionGroupId', 'left')
->join('`option`', '`option`.id=product_option.optionId')
->where('categoryId', $data['categoryId'])
->order_by('category_optiongroup.inOrder', 'ASC');
$query = $this->db->get();
return $query;
Related
I have been trying to show rows without duplicates but the query isn't working properly. I think the problem is one to many relationship, because one 'intervaloHorario' has many 'citas'. So, for example, i want to show only: 'From 8:00 to 15:00 (this is an intervaloHorario)' to date (cita) '27/08/1988'. What should i do?
Controller
$this->Fechacita_Model->delete_duplicaterow();
Model
public function delete_duplicaterow() {
$this->db->select('
c.intervaloHorario','ci.cita'
);
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario','left');
$this->db->group_by('c.idIntervaloHorario','ci.cita');
$query = $this->db->get();
return $query->num_rows();
}
Model(EDIT)
$this->db->select(array('c.intervaloHorario', 'ci.cita'));
$this->db->distinct();
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario', 'left');
$this->db->group_by('c.idIntervaloHorario', 'ci.cita');
$query = $this->db->get();
$this->db->last_query();
return $query->num_rows();
Database
Current database
Screenshot
Current list (unordered list but duplicates persists)
You can use $this->db->distinct() and add selecting primary key to remove duplicate:
public function delete_duplicaterow() {
$this->db->select(array('c.intervaloHorario', 'ci.cita'));
$this->db->distinct();
$this->db->from('intervaloshorarios c');
$this->db->join('citas ci', 'ci.idCitas = c.idIntervaloHorario','left');
$this->db->group_by('c.idIntervaloHorario','ci.cita');
$query = $this->db->get();
return $query->num_rows();
}
Use the keyword DISTINCT in your query
reference : https://dev.mysql.com/doc/refman/5.7/en/distinct-optimization.html
I applied the join query, but it seems to be not working. It's not selecting any data. When print_r the row_data is empty, but when I print_r $q it shows all the data,
public function getUserdata($id) {
$id=$id;
$query=$this->db->select('post_status.status_image')
->where('user_data.id',$id)
->from('user')
->join('user_data', 'user.id = user_data.id')
->join('post_status', 'user.id = post_status.user_id')
->get();
echo "<pre>";
print_r($query);
exit();
$q= $query->result_array();
return $q;
}
use where after join like this
$query=$this->db->select('post_status.status_image')
->from('user')
->join('user_data', 'user.id = user_data.id')
->join('post_status', 'user.id = post_status.user_id')
->where('user_data.id',$id)
->get();
also you can use toSql() instead of get() to check your raw query in phpMyAdmin. ;)
I have two tables. voting_ip and country. I want to retrieve results from country table where open_id_fk (foreign key) of voting_ip table is equal to open_id(Primary Key) of country table. How to write sql query to combine these queries and return the result. I am using the below code in my codeigniter model to retrieve number of occurances of open_id_fk in voting_ip table.
public function mostvotedans()
{
$this->db->select('open_id_fk, COUNT(*) as total');
$this->db->group_by('open_id_fk');
$this->db->order_by('total', 'desc');
$query = $this->db->get('voting_ip', 5);
return $query;
$query = $this->db->query("SELECT * FROM country WHERE open_id_fk=open_id;");
return $query;
}
change it as following.
public function mostvotedans()
{
$this->db->select('c.open_id,COUNT(ip.open_id_fk) as total')->from('voting_ip ip');
$this->db->join('country c','c.open_id=ip.open_id_fk');
$this->db->group_by('ip.open_id_fk');
$this->db->order_by('total', 'desc');
$this->db->limit(5);
$query = $this->db->get();
return $query;
}
Use a join statement :
$query = $this->db->select('v.open_id_fk, COUNT(v.*) AS total, c.country_name')
->join('country AS c', 'c.open_id = v.open_id_fk')
->from('voting_ip AS v')
->group_by('v.open_id_fk')
->order_by('total', 'DESC')
->limit(5);
Should work, I put 'country_name' because I don't know your tables.
I'm new to Codeigniter and PHP, just learning. I've searched for this but couldn't find the right answer. I have a table that contain some data, lets call it posts and in this table, each post have a category_id. I have another table called categories and in this table I have 2 columns: id and name.
I want to join the two tables that the result I'll get is the same as posts with another column called category_name that will be taken from categories.name. Just can't figure out how to do so.
This is what I was up to so far:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id == categories.id');
$query = $this->db->get();
return $query->result_array();
}
Thanks in advance.
========================================================================
EDIT:
After trying Adrian Forsius answer:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*, category.name AS category_name');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
I'm getting this error:
Error Number: 1054: Unknown column 'category.name' in 'field list'
SELECT *, `category`.`name` AS category_name
FROM (`posts`)
JOIN `categories` ON `posts`.`category_id` = `categories`.`id`
WHERE `category_id` IS NULL
ORDER BY `id` desc
LIMIT 20
====
EDIT 2:
So I found some mistakes in the code and fixed them, tried again, but still I get an error: (The mistakes were using $categoryID instead of $categoryId and select category.name instead of the true name of the table categories which is categories.name
Error Number: 1052: Column 'id' in order clause is ambiguous
SELECT *, `categories`.`name` AS category_name
FROM (`posts`)
JOIN `categories` ON `posts`.`category_id` = `categories`.`id`
WHERE `category_id` = '3'
ORDER BY `id` desc
LIMIT 20
This should do what you are looking for:
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*, categories.name AS category_name');
$this->db->from('posts')->order_by('posts.id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryId);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
replace
$this->db->select('*');
by
$this->db->select('posts.*');
$this->db->select('categories.name','category_name');
CI's documentation is pretty good.
Try this I think you forget to place = in place of ==
function getPostsWithByCategoryID($numberOfRows, $start, $categoryId)
{
$this->db->select('*');
$this->db->from('posts')->order_by('id','desc')->limit($numberOfRows, $start);
$this->db->where('category_id', $categoryID);
$this->db->join('categories', 'posts.category_id = categories.id');
$query = $this->db->get();
return $query->result_array();
}
I have announcements table, where I store announcements.catid (category id).
Categories table categories are used to be nested with the use of categories.pid.
What I am trying to do, is to get the categories nested one by one inside the multidimensional array.
Here is the image of how I want it to be inside highlighted part:
and here is the code:
public function get_mixed( $limit = NULL, $offset = NULL )
{
$this->db
->select('
announcements.id,
announcements.catid,
announcements.uid,
categories.title AS section,
categories.title AS category,
announcements.title,
announcements.text,
announcements.views,
announcements.created,
announcements.modified,
announcements.pubdate
')
->join('categories', 'announcements.catid = categories.id', 'left')
->join('categories as section', 'categories.pid = section.id', 'left')
->order_by('created DESC, '.$this->_order_by);
return $this->db->get( $this->_table_name, $limit, $offset )->result();
}
I tried first with by creating a section alias, but it gets me the same results.
Maybe there is a better approach ?
If you are aliasing one of the categories joins as section, then you would need to reference any fields you want from that categories table as section.[field] (for example, section.title)
Not knowing exactly what you want, my best guess at your revised query would be:
$this->db
->select('
announcements.id,
announcements.catid,
announcements.uid,
section.title AS section,
categories.title AS category,
announcements.title,
announcements.text,
announcements.views,
announcements.created,
announcements.modified,
announcements.pubdate
')
->join('categories', 'announcements.catid = categories.id', 'left')
->join('categories as section', 'categories.pid = section.id', 'left')
->order_by('created DESC, '.$this->_order_by);