mysql query returning different dates - mysql

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'];
}

Related

Long Boolean Where in SQL

CLOSED. I had a mistake somewhere else unrelated to the boolean.
What am I doing wrong here in this SQL where:
WHERE (country = 'HK') OR (country = 'TW') OR (country = 'AX')
It’s missing the result for AX in the output.
What am I doing wrong with this Boolean expression?
Note1. ' at end of AX was my typo here. I corrected above.
Full code here
$myresult = mysqli_query($GLOBALS['DBlink'],
"SELECT $getcolumn
FROM levermann
WHERE
( $sqlwhere2 ) AND
levermann.`WEEK` =
(SELECT `WEEK`, COUNT(*) AS cc
FROM levermann
WHERE ( $sqlwhere2 )
GROUP BY `WEEK`
HAVING cc > 4
ORDER BY `WEEK` DESC
LIMIT 1 )
AND $lswitch AND $marketcap
ORDER BY LScore2 DESC, MarketCAPUSD DESC, Stock_Short ASC
LIMIT 10;");
if ($region == 'ASIA') {
$sqlwhere2 = "
( country = 'HK' ) OR
( country = 'TW' ) OR
( country = 'AX' ) OR
( country = 'KS' ) OR
( country = 'SS' )";
$region='Asia';
}
if ($region == 'Global') {
$sqlwhere2 = " country like '%'";
$region='Global';
}
if ($region == 'US') {
$sqlwhere2 = " country = 'US'";
$region='US';
}
There is a special character at the end enclosing 'AX‘
Also why not just use in() instead?
WHERE country in ('HK','TW','AX')
Entire statement should be in brackets
Edit: also wrong quote at the end
WHERE (country = 'HK' OR country = 'TW' OR country = 'AX')
you have error in the condition
(country = 'AX‘)
you are not using the ending '' correctly.please correct and check it

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();

MYSQLi and Highcharts

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)
"

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;
}

Creating custom values/column within returned data set

I have a situation where I am using a UNION such as this:
$query = "SELECT bigimage, heading, fullarticle, dateadded FROM news ";
$query .= "WHERE (state = '" . $USER->state . "' OR state = 'ALL') AND status = 1 AND newstype != 1 AND frontpage = 1 AND bigimage > '' ";
$query .= "UNION ";
$query .= "SELECT image, eventname, details, dateadded FROM events ";
$query .= "WHERE (state = '" . $USER->state . "' OR state = 'ALL') AND status = 1 AND frontpage = 1 AND image > '' ";
$query .= "ORDER BY dateadded DESC LIMIT 10";
$restresult = mysql_query($query);
Now, what I am wanting to find out is which table each result comes from, so I was wondering if I could add some kind of custom result to each result returned to give me some form of indication what table it came from.
Could I add an extra "dynamic" column somehow in the returned result?
Try this:
$query = "SELECT bigimage, heading, fullarticle, dateadded, 'news' as TableName FROM news ";
$query .= "WHERE (state = '" . $USER->state . "' OR state = 'ALL') AND status = 1 AND newstype != 1 AND frontpage = 1 AND bigimage > '' ";
$query .= "UNION ";
$query .= "SELECT image, eventname, details, dateadded, 'event' FROM events ";
$query .= "WHERE (state = '" . $USER->state . "' OR state = 'ALL') AND status = 1 AND frontpage = 1 AND image > '' ";
$query .= "ORDER BY dateadded DESC LIMIT 10";
$restresult = mysql_query($query);
Sure. Stripping out the irrelevant php code, your query could be:
SELECT 'news' as type, bigimage, heading, fullarticle, dateadded FROM news
WHERE (state = ? OR state = 'ALL') AND status = 1 AND newstype != 1 AND frontpage = 1 AND bigimage > ''
UNION
SELECT 'event', image, eventname, details, dateadded FROM events
WHERE (state = ? OR state = 'ALL') AND status = 1 AND frontpage = 1 AND image > ''
ORDER BY dateadded DESC
LIMIT 10
This simply adds a constant column (I called "type") with different values (I used "news" and "event" after the table from which the row was selected) depending on which side on the union the row came from.