how to use DISTINCT here in my query CODEIGNITER - php

$q = $this->db->select('
books.title,
reserved_books.id,
reserved_books.isbn,
reserved_books.price,
reserved_books.item_sold,
reserved_books.date_sold,
reserved_books.total_amount
')
->from('reserved_books')
->join('books','reserved_books.isbn= books.isbn')
->limit($limit,$offset);
how can i use distinct here in my query?
reserved_books.isbn is the unique one.

Try Below:
$this->db->distinct();
but Distinct will not always work. You should add ->group_by("name_of_the_column_which_needs_to_be unique");
$this->db->group_by('column_name');

You don't need any join
$query=$this->db->distinct()->select('table_attribute')->where('condition')->get('table_name');
return $query->result();

Adds the "DISTINCT" keyword to a query before Select
$this->db->distinct();
Refrence

Here is a way to do this
$select = array(
'books.title',
'reserved_books.id',
'DISTINCT reserved_books.isbn',
'reserved_books.price',
'reserved_books.item_sold',
'reserved_books.date_sold',
'reserved_books.total_amount'
);
$q = $this->db
->select($select)
->from('reserved_books')
->join('books','reserved_books.isbn= books.isbn')
->group_by('reserved_books.isbn')
->limit($limit,$offset);

Here is best way to remove duplicate entry.
$query = $this->db->select("inf.id, inf.sku, inf.fab_id, inf.sku, inf.amount, inf.min_length, inf.num_of_pieces, inf.width ,inf.type")
->join("retailcat","retailcat.sku=SUBSTRING(inf.sku,1,19) AND retailcat.category=218","INNER")
->distinct()
->get("input_factor inf");

Related

Translating ORDER BY COUNT (*) DESC in codeigniter

function getCategory($year){
if(!$year){
$year=2017;
}
$q = $this->msdb->query("SELECT category_code, COUNT(*) AS numb
FROM easypm_sales_orders WHERE YEAR(sales_date) = $year
GROUP BY category_code ORDER BY COUNT(*) DESC");
return $q->result();
}
So, I have this model function and I want to translate the SQL query bit into codeigniter. I tried:
function getCategory($year){
if(!$year){
$year=2017;
}
$this->db->select('category_code');
$this->db->count("* as 'numb'");
$this->db->from('easypm_sales_orders');
$this->db->where('YEAR(sales_date)', $year);
$this->db->group_by('category_code');
$q = $this->db->order_by('numb', 'desc');
return $q->result();
}
but no luck, is there something I'm doing wrong?
try this
$query = $this->db
->select("category_code, count(*) AS numb",false)
->from ("easypm_sales_orders")
->where("YEAR(sales_date)",$year)
->group_by("category_code")
->order_by("numb","DESC")
->get();
return $query->result();
I believe it is necessary to use DB::raw() to represent a WHERE clause involving a function of a database column. Also, while it is possible to get a count from a Laravel query without using DB::raw(), if you want to assign a custom alias to that count it is also needed here. Consider the following code:
$this->db->select(DB::raw('count(*) as numb, category_code'))
$this->db->from('easypm_sales_orders');
$this->db->whereRaw('YEAR(sales_date) = ?', $year);
$this->db->group_by('category_code');
$q = $this->db->order_by('numb', 'desc');
return $q->result();
Codeigniter active record has no count method.
only count_all_results and count_all and they are used differently
You need replace your select options like this and remove $this->db->count("* as 'numb'");
$this->db->select('category_code');
$this->db->select('COUNT(*) numb');
Rest of your code OK.

How to write an SQL query in query CI syntax?

I have below query, which I need to write in Model of Codeignitor
SELECT DISTINCT make FROM equipment_nonconnected_master WHERE Equipment_NonConnected_Type_Master_ID IN (select Equipment_NonConnected_Type_Master_ID FROM equipment_nonconnected_type_master)
I am only aware of the select query simply written in Codeignitor as given below:
EXAMPLE:
$this->db->distinct();
$this->db->select('make');
$this->db->order_by('make', 'asc');
$query = $this->db->get('carriermodels');
Can someone help me writing the Query in CI syntax
You can do so by using simple where() function
$subquery="SELECT
Equipment_NonConnected_Type_Master_ID
FROM
equipment_nonconnected_type_master";
$this->db->distinct();
$this->db->select('make');
$this->db->from('carriermodels');
$this->db->where('Equipment_NonConnected_Type_Master_ID IN('.$subquery.')');
$this->db->order_by('make', 'asc');
$query = $this->db->get();
Or better to use join
$this->db->distinct();
$this->db->select('c.make');
$this->db->from('carriermodels c');
$this->db->join('equipment_nonconnected_type_master m','c.Equipment_NonConnected_Type_Master_ID =m.Equipment_NonConnected_Type_Master_ID ');
$this->db->order_by('c.make', 'asc');
$query = $this->db->get();

Select only unique values from a column in codeigniter

i have a table like this
name|subjects|location
......................
BUET|CSE|Dhaka
BUET|EEE|Dhaka
RUET|CE |Rajshahi
RU |CE |Rajshahi
here all the rows are distinct.And if I use
$this->db->select('*') and $this->db->distinct()
it would select all the rows of BUET but i only want like this
name|subjects|location
......................
BUET|CSE|Dhaka
RUET|CE |Rajshahi
RU |CE |Rajshahi
That means only the first column must be distinct and select all the columns as usual.And it would work if i use $this->db->select('name') and $this->db->distinct(). Then what would be about the other columns??
As my original table has many columns so I want to use $this->db->select('*'). I think $this->db->distinct() does not take any column as parameter. It differentiate result based on select. How can I do this?
Try like this
$this->db->select('DISTINCT `name`'); //You may use $this->db->distinct('name');
$this->db->select('*');
Select the distinct values by names.And your SELECT spelt wrong,may be its a typing mistake.And you can also use GROUP BY like
$this->db->select('*');
$this->db->group_by('name');
$this->db->select('*');
$this->db->group_by('column_name');
$this->db->from('tbl_name');
$query = $this->db->get();
return $query->result();
try this
You should use group_by in place of distinct.
because distinct will return unique rows. but to have unique column by row you should do group by.
Hope this helps.
$this->db->select('column names');
$this->db->distinct();
$query = $this->db->get('table name');
$query->result();
$this->db->select('job,id');
$this->db->group_by('job');
$query= $this->db->get('technicals')->result_array();
if(!empty($query)){
return $query;
}else{
return false;
}

Join query with multiple ON condition not working in codeigniter?

I need a query like this
SELECT * FROM (`users`)
LEFT JOIN `users_phone_numbers`
ON `users`.`id`= `users_phone_numbers`.`user_id`
LEFT JOIN `phone_number`
ON (`phone_number`.`id`= `users_phone_numbers`.`phone_num_id` AND users_phone_numbers.is_active = 1)
WHERE `users`.`id` = 56
i have code like this in codeigniter
$this->db->select('*');
$this->db->from('users');
$this->db->join('users_phone_numbers',
'users.id= users_phone_numbers.user_id',
'left');
$this->db->join('phone_number',
'(phone_number.id= users_phone_numbers.phone_num_id AND users_phone_numbers.is_active = 1)',
'left');
$this->db->where('users.id = '. $id);
$result = $q->result_array();
But i got this error
If you check Codeigniter's handling of the condition function, you will see the following:
// Strip apart the condition and protect the identifiers
if (preg_match('/([\w\.]+)([\W\s]+)(.+)/', $cond, $match))
Codeigniter is stripping the opening bracket from the query. Can you not move the LEFT JOIN additional clause within the WHERE clause to get around this?
EDIT:
$this->db->select('*');
$this->db->from('users');
$this->db->join('users_phone_numbers',
'users.id= users_phone_numbers.user_id',
'left');
$this->db->join('phone_number',
'phone_number.id= users_phone_numbers.phone_num_id)',
'left');
$this->db->where(array('users.id => '. $id, 'users_phone_numbers.is_active' => 1));
$result = $q->result_array();
You need to remove the second condition of the join.
$this->db->join('phone_number',
'phone_number.id= users_phone_numbers.phone_num_id AND users_phone_numbers.is_active = 1',
'left');
remove brackets like above. it works for me when there is no bracket

How to select all field from table with distinct (field x) ?? Codeigniter

I try this code
function catstore($id)
{
$this->db->distinct();
$this->db->select('store_id');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$result = $this->db->get();
return $result->result();
}
it produces only the distinct store_id but i want all column of the table with distinct rows based on store_id
Suggest me any option
Thanks in advance
You can try using group_by
function catstore($id)
{
$this->db->select('*');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$this->db->group_by('store_id');
$result = $this->db->get();
return $result->result();
}
I know this question is already long time ago, but one may be looking for the same problem like me, and this is how I solved it.
you can omit the select function altogether. If you are selecting all (*) from a table you do not need to use this function. When omitted, CodeIgniter assumes you wish to SELECT *
See http://codeigniter.com/user_guide/database/active_record.html#select
Or..
You can pass comma separated values to select function. So pass all column names separated by comma that you want to select
function catstore($id)
{
$this->db->distinct();
$this->db->select('store_id, column1, column2, cloumn3');
$this->db->from('products');
//$this->db->join('products','products.store_id = products.store_id');
$this->db->where('cat_id', $id);
$result = $this->db->get();
return $result->result();
}

Categories