Laravel count where row value > 0 - mysql

I've been trying to find a solution for my query for some time now. But i haven't been able to solve it yet. Most of it works nicely, but the count part does not work like i intend it to.
My query looks like this:
$years = Sample::whereCostumerId($id)
->groupBy('year')
->orderBy('year', 'DESC')
->get(array(
DB::raw('year'),
DB::raw('COUNT(id) as antalProver'),
DB::raw('MIN(provnr) AS minProvnr'),
DB::raw('MAX(provnr) AS maxProvnr'),
DB::raw('count(P_HCl) AS numP_HCl'),
DB::raw('count(Total_lerhalt) AS numLerhalt'),
DB::raw('ROUND(AVG(pH),1) AS avgpH'),
DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
DB::raw('AVG(X) AS coordExist')
));
The issue here is that many of the rows in the column P_HCl and Total_lerhalt contains zero. And i don't want to count these. I only want to count where value is greater than zero.
Im shure there's some nice solution for this.
If you guys have any other solution for the query all together id be happy to see it.
Thanks

You‘re most of the way there—you just need to add a where clause to your query:
$years = Sample::whereCostumerId($id)
->groupBy('year')
->orderBy('year', 'DESC')
->where('year', '>', 0)
->get(array(
DB::raw('year'),
DB::raw('COUNT(id) as antalProver'),
DB::raw('MIN(provnr) AS minProvnr'),
DB::raw('MAX(provnr) AS maxProvnr'),
DB::raw('count(P_HCl) AS numP_HCl'),
DB::raw('count(Total_lerhalt) AS numLerhalt'),
DB::raw('ROUND(AVG(pH),1) AS avgpH'),
DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
DB::raw('AVG(X) AS coordExist')
));

Related

Datatables - where with two conditions

I used the following statement to filter records by subject ids. (Here subject_id=5)
$this->datatables->where('letter_letter.subject_id', 5);
That is working perfectly. Further I want to filter records in subject_id range like 1 to 10. Then I changed my code as follows :
$this->datatables->where('letter_letter.subject', 10, '<');
But did not get the desired output. How can I edit my code to get expected result ? Can anyone help me ?
Just use two calls to where() to define the range:
$this->datatables->where('letter_letter.subject_id >= ', 1);
$this->datatables->where('letter_letter.subject_id <=', 10);
This is one way you can make a where query with two conditions:
$this->datatables->where("letter_letter.subject_id IS ? AND letter_letter.subject BETWEEN ? AND ?", 5, 1, 10)

How to get random row from MYSQL and how to write it in active records?

This is my query
"SELECT * FROM package_info ORDER BY RAND() LIMIT 0,3;"
I try to write it in active records like this.
$this->db->select('*');
$this->db->from('package_info');
$this->db->order_by("id", "random");
$this->db->limit(0, 3);
$result = $this->db->get();
But it is not work. How to write this in active record?
Use Below code it will work fine -
$this->db->select('*');
$this->db->from('package_info');
$this->db->order_by("id", "random");
$this->db->limit(3, 0);
$result = $this->db->get()->result();
// shows last executed query
echo $this->db->last_query();
// shows data fetched
echo "<pre>";
print_r( $result );
echo "</pre>";
You can visit on this link to view how queries are used in Codeigniter.
https://www.codeigniter.com/userguide2/database/active_record.html
CodeIgniter does not mandate that you provide 2 arguments for most Query Builder statements, you can do a whole WHERE by doing ->where('1=1') and its perfectly fine.
I'm surprised how many people don't understand method chaining, but I'll show it in my example it is just nicer...
$result = $this->db->select('*')
->from('package_info')
->order_by('rand()')
->limit(0, 3)
>get();
As per above, if you don't have 2 parameters in your original query, dont feel compelled to add two.
Another thing you can do with basic queries like these, is omit the from('package_info') entirely and stick the table name in the ->get('package_info')
If you cant be bothered with query builder you don't need to use it either. I don't for some things (you cannot use UNION with them for one). In this case just use
$result = $this->db->query("SELECT * FROM package_info ORDER BY RAND() LIMIT 0,3;");

Code Igniter - not showing the entry I need

I have the following code to get one line for each MAC with the LATEST state. The problem I have is that I get one line but not with the latest state but rather with the earliest.
function get_active_devices($min_duration, $max_duration)
{
//get all active devices DESC order
$this->db->distinct();
$this->db->group_by('mac');
$this->db->order_by("id", "desc");
$this->db->select('data.mac, state, time, iot_bo.notified, iot_bo.op_state, iot_bo.Name');
$this->db->where('time >', time()-$max_duration);
$this->db->where('time <', time()-$min_duration);
$this->db->join('iot_bo', 'iot_bo.mac = data.mac');
$this->db->where('iot_bo.op_state', '1');
$query = $this->db->get();
return $query;
}
Have you tried the query without the distinct and groupBy first? May be the result you want isn't in the total result set to begin with. Because there doesn't seem to be anything wrong with your use of db methods as it is.

Multiple column ORDERing in ZF?

i got this code
$select
->from(array("e" => "embarcacoes"))
->join(array("i" => "imagens"), 'e.id = i.barcoId')
->where("e.tipo = '{$this->view->tipoEmbarcacao}'")
->group("i.barcoId")
->limitPage($paginaAtual, $porPagina)
->order('e.prioridade DESC');
it works well, if i change the ->order('prioridade DESC'); line to ->order('id DESC'); it still works well, but if i try:
->order('prioridade DESC, id DESC'); or ->order(array('prioridade DESC','id DESC'));
it doesnt work. What is the correct syntax to make multiple orderign in zend framework? Thanks.
Got the solution,
i have to use like this
->order(array('e.prioridade DESC','e.id DESC'));
oh christ, such a beginner mistake.

min value from table

$test2=mysql_query("SELECT min(substr(status,1) FROM railways");
while($test_array1=mysql_fetch_array($test2)){
echo "<pre>";
print_r($test_array1);
echo "</pre>";
}
what is the correct form of this query...need to get the min value from the table itself..
substr(status, 1) returns the string as it is ... mysql counts in strings from 1, not from 0.
if i got you right using substr(status,2) is what you are looking for.
best regards
phil
substr(status, 1) is going to give 'w21', but substr(status, 2) is going to give 21. If you want to get the minimum and the pattern is DIGIT_NUMBER_NUMBER, you will use min(substr(status, 2)). From this data set: w21, c33, d55, d11, it will pull the minimum number you want.