I have a mysql table with results, and would like to add all the subject scores and store the result in another column called 'total' at the end of every student scores. How do I achieve this in codeigniter? Evert subject is a new column. Kindly assist.
$this->db->select('SUM(score) as total');
$q=$this->db->get('results');
$row=$q->row();
$score=$row->total;
SELECT sum(`maths`) + sum(`eng`) +sum('physics') as total FROM `tablename`
$this->db->select('sum(`maths`) + sum(`eng`) +sum(`physics`) as total',false);
$query=$this->db->get('table');
return $query->result();
Try like this
Related
Hi i have that query is it possible to insert additional count that counts self the Customer table rows? something like {{count($customers)}}
Also i have join the Services table on model, how can i add SUM on specific column of services table on the query above?
for example Service->sum('price');
$customers = Customer::query((['id', 'name', 'plate']))
->search($this->search,['id', 'name', 'plate'])
->withCount('services')
->orderBy($this->sortBy,$this->sortDirection,['id', 'name', 'plate'])
->paginate($this->perPage,['id', 'name', 'plate']);
return view('livewire.customers.show',['customers'=>$customers])
You do not need to change anything. The paginator already knows the total count.
$customers->total() should give you the count you're looking for.
As for the sum on a related column, Laravel added somewhat recently the withAggregates functions (withSum, withAvg). You should be able to get the total price by adding
->withSum('services as total_price', 'price')
to your query
The problem started because I have a table (Clientes), in which the primary key is not auto-incremental. I want to select the max value stored in a column database.
Like this select, but with eloquent ORM (Laravel):
SELECT MAX(Id) FROM Clientes
How can I do this?
I tried:
Cliente::with('id')->max(id);
Cliente::select('id')->max(id);
I prefer not to make a simple raw SELECT MAX(ID) FROM Clientes
I cannot make it.
Thanks all!
The correct syntax is:
Cliente::max('id')
https://laravel.com/docs/5.5/queries#aggregates
Laravel makes this very easy, in your case you would use
$maxValue = Cliente::max('id');
But you can also retrieve the newest record from the table, which will be the highest value as well
$newestCliente = Cliente::orderBy('id', 'desc')->first(); // gets the whole row
$maxValue = $newestCliente->id;
or for just the value
$maxValue = Cliente::orderBy('id', 'desc')->value('id'); // gets only the id
Or, if you have a created_at column with the date you could get the value like this
$maxValue = Cliente::latest()->value('id');
Relevant Laravel Documentation: https://laravel.com/docs/5.5/queries#aggregates
$maxValue = DB::table('Clientes')->max('id');
Cliente::where('column_name', $your_Valu)->max('id') // You get any max column
We can use the following code :
$min_id = DB::table('table_name')->max('id');
https://laravel.com/docs/8.x/queries#aggregates
Let's consider the image above. I would like to show duplicated entries as one entry and also I want to show the sum of the "stock" column. In this case it should be 5722.
Is it possible to do it using Eloquent? Or what are the best ways to do it?
Not sure how your database / query is built but you could maybe use something like that:
Item::groupBy('item_name')
->selectRaw('*, sum(stock) as sum')
->get();
It will return a collection of Item with an additional “sum” field
This will group the result with same medicine and supplier name and sum up the stock.
$result = Model_Name::groupBy('medicine_name','supplier_name')
->selectRaw('*, sum(stock) as sum')
->get();
Try this. Hope it might help you.
I want to get custom column with custom value with each row selection of table using codeigniter active record. In normal MySql selection query i can do that, but using active record custom column is unknown. SO how to do that? for any help thanks.
$this->db->select('name,\'custom_col\'')->from('my_table')->get();
You can do like this:
$this->db->select("'custom_col',name",FALSE)->from('my_table')->get();
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
You can also do like this
$this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE);
$query = $this->db->get('mytable');
Check this link
https://ellislab.com/codeigniter/user-guide/database/active_record.html#select
I just started with codeigniter and I'm stuck in my database query. I created a simple survey and I want to get the average of the columns. Right now I'm doing it like this to test it:
for($x=1;$x<=5;$x++){
$this->db->select_avg('q'.$x , 'averageq'.$x);
}
$this->db->where('sex','male');
$query = $this->db->get('survey');
$data = $query->row_array();
print_r($data);
The survey will comprise of 30+ questions, is there a shorter way getting the the average of each column? something like:
select_avg('all columns in my answers table);
thank you