Convert this sql code to codeigniter? - mysql

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

Related

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

How to compare two dates MySQL

I'm trying to create a report function, I store in my database some data with a specific value (closeDate), this value can be only 7th or 21nd, here is an example of my table:
What I want to do is every 22nd execute a query to select all the register which have Status = "Active" or "On approve" and CloseDate < to 22nd (Curdate). I have this query, but this query show me all exactly like in the image and what I expect is that the query show me only the data from Id = 00001 and 00003 because Id 00002 CloseDate(2016-10-21) it's not < to 2016-09-22 (curdate).
SELECT tbl_usuarios.Correo, tbl_alertas.Id, tbl_alertas.Purpose, tbl_alertas.Status, tbl_alertas.OpenDate, tbl_alertas.CloseDate, tbl_alertas.Owner, tbl_alertas.ValueStream, tbl_alertas.Family
FROM tbl_usuarios
INNER JOIN tbl_alertas ON tbl_usuarios.Nombre = tbl_alertas.Owner
WHERE STATUS = 'On approve' OR STATUS = 'Active' AND CloseDate < CURDATE()
What I'm doing wrong?
You may need to rearrange your clauses in your WHERE condition. It has both OR and AND so it may not be interpreted as you expect.
I would try something like this:
WHERE (STATUS = 'On approve' OR STATUS = 'Active') AND CloseDate < CURDATE()
Or you can simplify this using IN:
WHERE STATUS IN ('On approve', 'Active') AND CloseDate < CURDATE()
SELECT tbl_usuarios.Correo, tbl_alertas.Id, tbl_alertas.Purpose, tbl_alertas.Status, tbl_alertas.OpenDate, tbl_alertas.CloseDate, tbl_alertas.Owner, tbl_alertas.ValueStream, tbl_alertas.Family
FROM tbl_usuarios
INNER JOIN tbl_alertas ON tbl_usuarios.Nombre = tbl_alertas.Owner
WHERE (STATUS = 'On approve' OR STATUS = 'Active') AND DATE(CloseDate) < CURDATE()

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

Select data from 2 different tables

Instead of writing 2 different queries, I'd like to do in one
$query = $pdo->prepare("
SELECT Car_Name
FROM Car_data
WHERE Car_Code = :carCode
SELECT Fruit_Name
FROM Fruit_Data
WHERE Fruit_Code = :fruitCode;
");
$query->bindParam(':carCode', $header['Car_Code']);
$query->bindParam(':fruitCode', $header['Fruit_Code']);
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
if (!empty($result)) {
}
I expect to receive data in the following format:
if both tables has data:
Car_Name = 'Golf'
Fruit_Name = 'Banana'
if only one table has data:
Car_Name = 'Golf'
Fruit_Name = ''
OR
Car_Name = 'Golf'
How can I achive that?
$query = $pdo->prepare("
SELECT (SELECT Car_Name
FROM Car_data
WHERE Car_Code = :carCode ) as Car_Name ,
(SELECT Fruit_Name
FROM Fruit_Data
WHERE Fruit_Code = :fruitCode) as Fruit_Name;
");
use subqueries, this should work