I have a database table with separate date and time column. To get particular I execute query in mysql in following manner
select * from table_A where empID='A1201' order by TIMESTAMP(date,time) desc limit 1
How can I convert this particular for codeigniter? I tried in following manner but it's not working
$column = 'TIMESTAMP(date,time)';
$this->db->select('*');
$this->db->where('empID', 'A1201');
$this->db->from('table_A');
$this->db->order_by($column, 'desc');
$this->db->limit(1);
$query = $this->db->get();
$data = $query->result();
return $data;
This query result in error since it executes in following manner ORDER BY TIMESTAMP(date DESC, time) DESC while the correct way is ORDER BY TIMESTAMP(date,time) DESC. What will be the correct way for codeigniter using active record
try this
$query = $this->db
->select('*')
->where('empID', 'A1201')
->from('table_A')
->order_by($column, 'desc', false)
->limit(1)
->get();
order by comes with a 3rd option - just set this to false
You can get more infos about that in their documentation here
Related
please help me,
i have query:
SELECT * from (select * from products ORDER BY id DESC LIMIT 20) AS result ORDER BY discount DESC LIMIT 14
so, how to convert into query builder in laravel 5.
tks!
Using the query builder and importing (use) DB, allows you to build and fetch the result you want.
use DB;
$result = DB::table(
DB::raw("(" .
DB::table('products')
->select('*')
->orderBy('id', 'desc')
->limit(20)
->toSql()
. ") as result"
)
)
->select('*')
->orderBy('discount', 'desc')
->limit(14)
->get();
table() selects the table you want to query. In this case we are building up a separate SQL statement to fetch the table.
select() the columns you would like to see.
orderBy($column, $direction) where $column is the name of the column you would like to order and $direction is the order, desc or asc.
limit($limit) only return $limit items to the result set.
toSql() returns the current QueryBuilder SQL statement as a string.
get() returns the data in a Collection.
Also added in the missing discount ordering.
Try something like:
Products::orderBy('id', 'desc')->take(20)->orderBy('discount', 'desc')->take(14)->get();
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.
i want to search in two columns and select last record and send it as array to controller to show data
public function show_data($post_code,$fname){
$this->db->where('post_code',$post_code);
$this->db->where('fname',$fname);
$query=$this->db->get('customer');
return $query->result_array();}
it finds my desire records but i want to show last record, i used order_by(), limit() function but they don't work properly and affected my data entries in database. what's wrong? how can i correct it?
thank you
my solution is :
public function show_data($post_code,$fname){
$this->db->where('post_code',$post_code);
$this->db->where('fname',$fname);
$this->db->order_by("id", "desc");
$this->db->limit(1);
$query=$this->db->get('customer');
return $query->result_array();
}
thank you saty
$query = $this->db->query("SELECT * FROM TABLE_NAME WHERE COLUMN_NAME=$COLUMN_NAME AND SECOND-COLUMN_NAME=$SECOND-COLUMN_NAME ORDER BY id DESC LIMIT 1")->result();
return $query;
This will return you last row you created.
I'm trying to use data inside some eager loading in Laravel.
I have a "Sensors" table with many "Measurements" in another one.
I need to check every measurement a sensor has every hour. When I do it, it saves the last id of the measurement checked inside "sensors.last_checked_measurement"
$sensors = Sensor::with(array('measurements' => function($query) {
$query->where('id', '>', SENSOR.LAST_CHECKED_MEASUREMENT); // <-- in between data
}))->get();
This, as you may know, translate into
"select * from 'sensors'"
"select * from `measurements` where `measurements`.`sensor_id` in (?, ?) and `id` > ?"
If Laravel can use the result of the first query to get the sensor_id for the next one, maybe it can be done, right?
So my question:
How can I use that "SENSOR.LAST_CHECKED_MEASUREMENT" inside the eager loading?
Have you tried db::raw?
$sensors = Sensor::with(array('measurements' => function($query) {
$query->where('id', '>', DB::raw('SELECT LAST_CHECKED_MEASUREMENT FROM SENSOR ORDER BY ID DESC LIMIT 1'));
}))->get();
i have mysql query like this:
SELECT * FROM ms_project_log
INNER JOIN ms_project ON ms_project_log.iwo_no = ms_project.iwo_no
WHERE ms_project_log.iwo_no = '0007/NMS/BOTM/01/12'
ORDER BY ms_project_log.log_date DESC
how to convert to codeigniter active record?
here's a code for you
$this->db->select('*');
$this->db->from('ms_project_log');
$this->db->join('ms_project','ms_project_log.iwo_no=ms_project_iwo_no', 'inner');
$this->db->where(ms_project_log.iwo_no, '0007/NMS/BOTM/01/12');
$this->db->order_by("ms_project_log.log_date", "desc");
$this->db->get();
$this->db->join('ms_project','ms_project_log.iwo_no = ms_project.iwo_no','inner')
->order_by('ms_project_log.log_date','desc')
->get_where('ms_project_log', array('ms_project_log.iwo_no'=>'0007/NMS/BOTM/01/12'))
->result_array();
You can filter selected column using select method, by default if not using select method your query will use "select *"