symfony2: select from Table where start=today - mysql

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

Related

How to merge 2 column into 1 column TO output in SQL

I have this sql query:
$sql = "SELECT d.doctor_name, a.patient_name, s.date, s.time FROM appointments AS a LEFT JOIN doctors AS d ON d.id = a.doctor_id LEFT JOIN slots AS s ON s.id = a.slot_id WHERE s.date > '2019-10-01' ORDER BY d.doctor_name DESC ";
$result = $mysqli->query($sql);
echo '<pre>';
while ( $row = $result->fetch_assoc() ) {
print_r($row);
}
echo '</pre>';
and it's output is:
Array
(
[doctor_name] => Khaled
[patient_name] => Nawaz
[date] => 2019-10-11
[time] => 02:01
)
Array
(
[doctor_name] => Khaled
[patient_name] => Anik
[date] => 2019-10-07
[time] => 02:31
)
Array
(
[doctor_name] => Khaled
[patient_name] => Manik
[date] => 2019-10-02
[time] => 03:31
)
Can I merge the date and time column as slot_date_time?
So output will be e.g:
Array
(
[doctor_name] => Khaled
[patient_name] => Manik
[slot_date_time] => 2019-10-02 03:31
)
Try this query
SELECT d.doctor_name, a.patient_name, CONCAT(s.date, ',', s.time) AS slot_date_time
FROM appointments AS a LEFT JOIN doctors AS d
ON d.id = a.doctor_id LEFT JOIN slots AS s
ON s.id = a.slot_id
WHERE s.date > '2019-10-01'
ORDER BY d.doctor_name DESC
Use CONCAT for merge two column as one column

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

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

SQL, MySQL and php

I have 2 tables
CREATE TABLE `employee` (
`Employee_ID` smallint(4) NOT NULL AUTO_INCREMENT,
`Name` varchar(25) NOT NULL DEFAULT '',
PRIMARY KEY (`Employee_ID`)
) ;
--- -----------------
CREATE TABLE `attendance` (
`Attendance_ID` int(9) NOT NULL AUTO_INCREMENT,
`Employee_ID` smallint(3) NOT NULL,
`Date` date NOT NULL,
`Attendance` char(1) NOT NULL DEFAULT '',
PRIMARY KEY (`Attendance_ID`)
) ;
For Attendance field it's a "P" when the employee is present "L" when late and "A" when absent.
I want to make a query that crosses the dates with employees names and show atendance.
Something similar to:
Attendance 07/02/2015 14/02/2015 21/02/2015 ...
Diane P P L
Robert A P P
Karine P P A
...
I didn't post any query because actually I failed many times to figure out how to
Thanks for help
I do not know how to get dynamic columns in MySQL. This is how I would do it:
SELECT attd.Date, attd.Attendance, empl.Name FROM attendance attd, employee empl WHERE empl.Employee_ID = attd.Employee_ID
That will give result:
Then I would use server side code (example PHP):
<?php
$attendance = array();
foreach ($query as $row) {
if (isset($attendance[$row['Name']])) {
$attendance[$row['Name']][$row['Date']] = $row['Attendance'];
} else {
$attendance[$row['Name']] = array($row['Date'] => $row['Attendance']);
}
}
print_r($attendance);
/* Array(
"Diane" => Array("2015-02-07" => 'P', "2015-02-14" => 'P'),
"Robert" => Array("2015-02-07" => 'P', "2015-02-14" => 'P'),
"Karine" => Array("2015-02-07" => 'L', "2015-02-14" => 'L')
)
*/
select
a.Employee_ID,
min(e.Name) as Employee_Name,
case when a.Date = dateadd(:basedate, interval 7*0 day) then min(a.Attendance) end as d0 /* d for data? */
case when a.Date = dateadd(:basedate, interval 7*1 day) then min(a.Attendance) end as d1
case when a.Date = dateadd(:basedate, interval 7*2 day) then min(a.Attendance) end as d2
case when a.Date = dateadd(:basedate, interval 7*3 day) then min(a.Attendance) end as d3
dateadd(:basedate, interval 7*0 day) as week0
dateadd(:basedate, interval 7*1 day) as week1
dateadd(:basedate, interval 7*2 day) as week2
dateadd(:basedate, interval 7*3 day) as week3
from
attendance as a inner join employee as e ...
where a.Date between :basedate and dateadd(:basedate, interval 7*3 day)
group by
a.Employee_ID
You could pass in a :basedate parameter and offset a fixed number of weeks from that point. (I don't know what's the parameter convention for PHP and MySQL.) This will work if you can fix your results to a set number of weeks.
Pick out the dates from the first row to build the header and then build the rest of the table as you normally would. Yes there's some redundant data but there's no easy way to alias the columns with the dates themselves.
Following query you can use to gain the shown output along with a bit of PHP application
SELECT E.Name, A.Date, A.Attendance
FROM attendance AS A
INNER JOIN employee AS E
ON A.Employee_ID = E.Employee_ID
ORDER BY A.Date, E.Name
For implementing this in PHP
<?php
if (!$link = mysql_connect('< mysql_host >', '< mysql_user >', '< mysql_password >')) {
echo 'Could not connect to mysql';
exit;
}
if (!mysql_select_db('< mysql_dbname >', $link)) {
echo 'Could not select database';
exit;
}
$sql = 'SELECT E.Name, A.Date, A.Attendance '.
'FROM attendance AS A '.
'INNER JOIN employee AS E '.
'ON A.Employee_ID = E.Employee_ID '.
'ORDER BY E.Name, A.Date';
$result = mysql_query($sql, $link);
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
$name = '';
$resultArray = array();
$attendance = array();
foreach ($query as $row) {
if (isset($attendance[$row['Name']])) {
array_push($attendance[$row['Name']], $row['Date'] => $row['Attendance']);
} else {
$attendance[$row['Name']] = array($row['Date'] => $row['Attendance']);
}
}
print_r($attendance);
mysql_free_result($result);
?>
You would get an a associative array like this
Array(
"Diane" => Array("2015-02-07" => 'P', "2015-02-14" => 'P'),
"Robert" => Array("2015-02-07" => 'P', "2015-02-14" => 'P'),
"Karine" => Array("2015-02-07" => 'L', "2015-02-14" => 'L')
)
Now you can use this associative array to in a loop to render these elements in the Web page
Hope this helps...

filter rows based on column values in mysql query

This is my mysql query
SELECT a.model_name,
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year,
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b
WHERE a.model_id = b.model_id AND a.model_status = '1'
ORDER BY a.model_created_on DESC
I want to do one more filtering option in this query. I need to get the records based on release_year = 1 & release_year = 1. I have done release_year and release_month columns through IF STATEMENT in MYSQL QUERY
release_year
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year
release_month
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
How do I get the records based on these values (release_month = 1 & release_year = 1) in this query? I have tried WHERE release_month = 1 AND release_year = 1 but this one returns unknown column
You could do this:
SELECT
*
FROM
(
SELECT
a.model_name,
a.model_created_on,
IF( b.officially_released_year = '".$currentyear."',1,0 ) AS release_year,
IF( b.officially_released_month = '".$first_month."' OR b.officially_released_month = '".$second_month."' OR b.officially_released_month = '".$third_month."' OR b.officially_released_month = '".$currentmonth."' ,1,0) AS release_month
FROM ".TBL_CAR_ADD_MODELS." a, ".TBL_CAR_SPEC_GENERAL." b
WHERE a.model_id = b.model_id AND a.model_status = '1'
) AS tbl
WHERE tbl.release_year=1 AND release_month=1
ORDER BY tbl.model_created_on DESC