MYSQLi and Highcharts - mysql

I am having trouble with my MYSQL query. I can send it to Highcharts but is is not quite right.
I want to get last months temperature and put it in Highcharts.
My query sort of works but only gets the first temperature for each day, and that is just after midnight. How do I make a query that will get hourly temperatures daily for a month?
This is my query. I am sorry I cannot get this formatting correct.
<?php
$con = mysqli_connect("localhost","root","root","manortsc_test");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$sth = mysqli_query($con,"
SELECT DateTime,T
FROM alldata
WHERE YEAR(DateTime) = YEAR(CURDATE()) AND MONTH(DateTime) = MONTH(CURDATE() - INTERVAL 1 MONTH)
GROUP BY YEAR(DateTime), MONTH(DateTime), DAY(DateTime)
"
);
$rows = array();
$rows['name'] = 'Outside';
while($r = mysqli_fetch_array($sth)) {
$rows['data'][] = $r['T'];
}
$sth = mysqli_query($con,"
SELECT DateTime,Ti
FROM alldata
WHERE YEAR(DateTime) = YEAR(CURDATE()) AND MONTH(DateTime) = MONTH(CURDATE() - INTERVAL 1 MONTH)
GROUP BY YEAR(DateTime), MONTH(DateTime), DAY(DateTime)
"
);
$rows1 = array();
$rows1['name'] = 'Inside';
while($rr = mysqli_fetch_assoc($sth)) {
$rows1['data'][] = $rr['Ti'];
}
$result = array();
array_push($result,$rows);
array_push($result,$rows1);
print json_encode($result, JSON_NUMERIC_CHECK);
mysqli_close($con);
?>

Spent all night researching and came up with the answer.
It simply gets the maximum T each day for the month. I can build on this.
Hope it helps someone.
SELECT DateTime,max(T)
FROM alldata
WHERE YEAR(DateTime) = YEAR(CURDATE()) AND MONTH(DateTime) = MONTH(CURDATE() - INTERVAL 1 MONTH)
GROUP BY day(DateTime)
"

Related

Convert this sql code to codeigniter?

How do i convert this sql code to CodeIgniter?
SELECT *,sum(sales_total) as daily_report FROM `tbl_sales`,(select
sum(sales_total) as month from tbl_sales where date_format(sales_date,'%m') = '01') as totals where sales_date = '2018-01-18'
$sub_query_from = "tbl_sales,(SELECT SUM(sales_total) AS month FROM tbl_sales WHERE date_format(sales_date,'%m') = '01') AS totals";
$this->db->select('*, SUM(sales_total) AS daily_report');
$this->db->from($sub_query_from);
$this->db->where('sales_date','2018-01-18');
$query = $this->db->get();

How to pivot my table in mysql

I have table database like this :
i want to make on table view like this :
Thanks.
I assume you need it per year basis.
Select year, id_lesson,
Sum(case when term = 1 then exam_1 end) exam_1_term_1,
Sum(case when term = 1 then exam_2 end) exam_2_term_1,
Sum(case when term = 2 then exam_1 end) exam_1_term_2,
Sum(case when term = 2 then exam_2 end) exam_2_term_2,
Sum(case when term = 3 then exam_1 end) exam_1_term_3,
Sum(case when term = 3 then exam_2 end) exam_2_term_3
From your_table
Group by id_lesson, year;
Try this logic, Hope it is help.
$qdata = mysqli_query("SELECT * FROM table ORDER BY ID_Lesson");
$rows = array();
if (mysqli_num_rows($qdata) > 0) {
// Store all exam based on ID_Lesson ar array
while ($data = mysqli_fetch_array($qdata)) {
if (!isset($row[$data['ID_Lesson']])) {
$rows[$data['ID_Lesson']] = array($data['ID_Lesson'], $data['Exam1'], $data['Exam2']);
}
else {
$rows[$data['ID_Lesson']][] = $data['Exam1'];
$rows[$data['ID_Lesson']][] = $data['Exam2'];
}
}
$html = '';
// Print per element as <tr> row
foreach ($rows as $k => $row) {
$html .= "<tr><td>" . implode("</td><td>", $row) . "</td></tr>";
}
}

mysql query returning different dates

I am trying to add a new feature to my weather station, date of highest temperature and the date it rained the most.
Why do does this code give different dates in the result?
If I changed the first query to either T, R, P or H, the maximum for the row are shown but the date is always the same.
Once I understand that, I may be able to fix other problems with the code.
This is the output
$result = mysqli_query($con,"
SELECT DateTime,max(Tmax)
FROM alldata
WHERE YEAR(DateTime) = YEAR(NOW())
"
);
while($row = mysqli_fetch_array($result)){
$maxtempDate1 = $row['DateTime'];
$tempMax1 = $row['max(Tmax)'];
}
$result = mysqli_query($con,"
SELECT DateTime,Tmax
FROM alldata
WHERE Tmax=(select max(Tmax) from alldata)
AND YEAR(DateTime) = YEAR(NOW())
"
);
while($row = mysqli_fetch_array($result)){
$maxtempDate = $row['DateTime'];
$tempMax = $row['Tmax'];
}
$result = mysqli_query($con,"
SELECT DateTime,Tmax
FROM alldata
WHERE YEAR(DateTime) = YEAR(NOW())
ORDER BY Tmax DESC
LIMIT 1
"
);
while($row = mysqli_fetch_array($result)){
$maxtempDate = $row['DateTime'];
$tempMax = $row['Tmax'];
}

I should compare students by month

public function getComparisonStudentsByMonth()
{
$last_month = date('m', strtotime('-2 month'));
$previus_month = date('m', strtotime('-1 month'));
return
$this->db->select('MONTH('student_add_date')', date('m'));
$this->db->where('student_add_date <= DATE_ADD(NOW(),INTERVAL 30 DAYS)');
$query = $this->db->get('students')->result();
}
Problem
User every month inserting new students and admin page should show the comparison in percentage between months. I should take last month like 100% and compare with current month. I understand the logic but how to do with MySql.
To get all percentage of every month you can use this:
SELECT (MONTH(student_add_date)) AS month_temp, ((count(*) /(SELECT count(*) FROM students)) * 100)percentage from students group by month_temp
to get between two month you need to specify month in query. for example i will search in september(9) and oktober(10)
SELECT
(MONTH(student_add_date)) AS month_temp,
((count(*) /(SELECT count(*) FROM students WHERE MONTH(student_add_date) = 9 OR MONTH(student_add_date) = 10)) * 100)percentace
from students
WHERE
MONTH(student_add_date) = 9
OR
MONTH(student_add_date) = 10
group by month_temp
In codeigniter you can run custom query like this:
$query = $this->db->query('SELECT
(MONTH(student_add_date)) AS month_temp,
((count(*) /(SELECT count(*) FROM students WHERE MONTH(student_add_date) = 9 OR MONTH(student_add_date) = 10)) * 100)percentace
from students
WHERE
MONTH(student_add_date) = 9
OR
MONTH(student_add_date) = 10
group by month_temp');
$result = $query->result_array();
// var_dump($result);
return $result;
public function getComparisonStudentsByMonth()
{
$last_month = date('m', strtotime('-2 month'));
$previus_month = date('m', strtotime('-1 month'));
$last_month = $this->db->where('MONTH(student_add_date)', $last_month)
->count_all_results('students');
$previus_month = $this->db->where('MONTH(student_add_date)', $previus_month)
->count_all_results('students');
$result = $previus_month * 100 / $last_month;
return $result;
}

symfony2: select from Table where start=today

The "start" is a dateTime. I'm trying to get all rows that have "start" = Today without considering the hours and mins.
This what I did so far.
Only events with this form 'Y-m-d 00:00:00' are selected
$em = $this->getDoctrine()->getManager();
$onthisday = $em->createQuery('
SELECT e
FROM ECMEventBundle:Events e
WHERE e.start = CURRENT_DATE()
AND e.eventcreator = :userId'
)->setParameter ( 'userId', $user->getId () );
$todayEvent = $onthisday->getResult();
Any response will be appreciated.
you can use php DateTime class:
$myDate = new \DateTime();
$em = $this->getDoctrine()->getManager();
$onthisday = $em->createQuery('
SELECT e
FROM ECMEventBundle:Events e
WHERE e.start > :myDate
AND e.eventcreator = :userId'
)->setParameter ( 'userId', $user->getId () )
->setParameter ( 'myDate', $myDate->format('Y-m-d 0:0:0') );