php mysql group by date with yyyy-mm-dd format - mysql

I had a mysql table called events with the fields: id, date, and name.
The date field has the format yyyy-mm-dd hh::mm:ss edit: meaning it is in datetime format
I want to group the events by day, and I wasn't sure how to approach this- is there a way to select only the month and day from the field? or should i use PHP after I select all the "events"
my end goal is to have something like this:
March 10th:
event1,
event2
March 11th:
event4,
event5
I found MySQL select using datetime, group by date only but I'm not sure how to implement it:
SELECT DATE_FORMAT(date, '%H%i'), DATE_FORMAT(date, '%M %D'), name FROM events ORDER BY date
Thanks!
EDIT:
ended up using this:
$sql = "select team1, team2, DATE_FORMAT(date,'%Y-%m-%d') as created_day FROM games WHERE attack = '1' GROUP BY created_day";
$result = mysql_query($sql);
$curDate = "";
while (list($team1, $team2, $date) = mysql_fetch_row($result))
{
if ($date != $curDate)
{
echo "$date --------\n";
$curDate = $date;
}
echo "game data: $team1 $team2";
}

If you use group by you will not get one row out of it. So the way you want is not possible through Group By AFAIK.
$query = "SELECT distinct(DATE_FORMAT(date, '%M %D')) as d FROM yourtable";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
echo $row['d']
$sql = "SELECT * FROM yourtable WHERE DATE_FORMAT(date, '%M %D')='$row[d]'";
$rs = mysql_query($query);
while($r = mysql_fetch_assoc($rs)) {
echo "event";
}
}

You should indeed use php to get this done. But since most of current system sepate logic from display, I'd use only one pass and not (NUMBER OF DAYS + 1) SELECTs, and prepare an array that I can reuse later for my display.
$query = "SELECT DATE_FORMAT(date, '%M %D') as d, name FROM yourtable ORDER BY date";
$foo=array();
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
//some logic to test if it's safe to add the name
$foo[$row['d']][]=$row['name'];
}
And then when i'd need it (through a template or your "view")
foreach($foo as $date => $events) {
echo $date . ":\n\t";
echo implode(",\n\t", $events);
echo "\n";
}
so it fits the format you set to yourself.
Hope that helped

I think from that question/answer, you can get something like this instead
March 10th, event1
March 10th, event2
March 11th, event4
March 11th, event5
It does not really 'group' dates as your wish but I think you can use php to continue from this result.

I agree with Kharaone, separate logic from display. That being said, I think that something similar to this query might be what you are looking for:
SELECT A FROM
(
SELECT DATE_FORMAT(date,'%M %D:') AS A, DATE(date) AS B, 1 AS C FROM games GROUP BY DATE(date)
UNION ALL
SELECT name AS A, DATE(date) AS B, 2 AS C FROM games
) X
ORDER BY B, C ASC;

Related

database query to get lowest price based on last crawel date

I would like to get lowest price of product based on last crawled dates by various resellers. My current function is very basic, it gets me lowest price from table without considering reseller ids and crawled timestamps.
I've rough idea that we can SELECT * FROM "custom_data_table" and process the data using php. Please have a look at attachment for further clarification.
function get_lowest_price($table_id) {
global $wpdb;
$table_prices = $wpdb->get_results(
$wpdb->prepare(
"SELECT price FROM `custom_data_table` WHERE tableid= %d"
,$table_id)
);
if (!empty($table_prices) && $table_prices !== NULL)
return rtrim(min($table_prices)->price, '00');
}
The right query here is:
SELECT price
FROM custom_data_name cdn, (
SELECT MAX(crawled) AS maxCrawled, resellerid
FROM custom_data_name
GROUP BY resellerid
) cdnFiltered
WHERE cdn.crawled = cdnFiltered.maxCrawled AND
cdn.resellerid = cdnFiltered.resellerid AND
tableid = %d;
Try this:
SELECT B.price
FROM (SELECT resellerid, MAX(crawled) max_crawled
FROM custom_data_table
GROUP BY resellerid) A
JOIN custom_data_table B
ON A.resellerid=B.resellerid AND A.max_crawled=B.crawled;
Maybe use ORDER BY crawled and LIMIT 1

SQL Doctrine year's eve

that's is strange, maybe is my fault. Today I launch my test and 2 of they faults (yesterday dont). This test use a control date of some bookings, and I presume the problem is that today is 31/12. I'll show you the code:
$em = $this->getEntityManager();
$query = $em->createQuery(
'SELECT b
FROM AppBundle:Booking b
WHERE b.bookingDate >= CURRENT_DATE()
AND b.bookingDate <= CURRENT_DATE()+1
ORDER ASC b.bookingDate'
)
return $booking = $query->getResult();
That way is the only way i found to check that the booking have a date at today. Is possible that this fault becouse today is 31/12? Do you have some solution?
sorry for bad english, thanks.
You can calculate the dates by Php:
$today = new DateTime('now');
$tomorrow = new DateTime('tomorrow');
$em = $this->getEntityManager();
$query = $em->createQuery(
'SELECT b
FROM AppBundle:Booking b
WHERE b.bookingDate >= :today
AND b.bookingDate < :tomorrow
ORDER BY b.bookingDate ASC'
);
return $query->setParameters(array(
'today' => $now->format('Y-m-d'),
'tomorrow' => $tomorrow->format('Y-m-d')
))->getResult();
You can avoid var $booking and return result directly.
Also you have missed ; at the end of createQuery and ORDER is ORDER BY.
Also beware with namespaces, maybe you must to use new \DateTime('now');
CURRENT_DATE()+1
returns
20161232
which seems to be pretty wrong.
A way to make it works as you want it to would be this:
DATE_ADD(CURRENT_DATE(), INTERVAL 1 DAY);
which returns
2017-01-01

MySQL Running Total in JSON Output

Have been looking at this for ages, but cannot overcome the problem. Have found running total questions, but none deal with 2 queries...
$query = "SELECT d.DateAdded, #running_sum:=#running_sum + d.count AS running FROM (SELECT DateAdded, COUNT(*) AS 'count' FROM details WHERE MemberStatus = 'Active'GROUP BY DATE_FORMAT(DateAdded,'%Y-%m-%d') ORDER BY DateAdded ) d JOIN (SELECT #running_sum := 0 AS dummy) dummy;";
$result = mysqli_query($mysqli,$query);
$array = array();
while ($row2 = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$RunningTotal = $row2['running'];
foreach($mysqli->query('SELECT DateAdded, COUNT(*) FROM details GROUP BY DATE_FORMAT(DateAdded,"%Y-%m-%d")') as $row) {
$year = date("Y-m-d",strtotime($row['DateAdded']));
array_push($array,array('Year'=>$year, 'Numb'=>$row['COUNT(*)'],'Total'=>$RunningTotal));
}
}
echo json_encode($array);
Above is the closest I've come, but cannot get the output correct..
Current output of above is.. (obviously I have WHILE and FOREACH completely wrong!!)
[{"Year":"2016-10-27","Numb":"1","Total":"1
"},{"Year":"2016-10-31","Numb":"1","Total":"1
"},{"Year":"2016-11-02","Numb":"1","Total":"1
"},{"Year":"2016-11-05","Numb":"1","Total":"1
"},{"Year":"2016-11-07","Numb":"1","Total":"1
"},{"Year":"2016-11-08","Numb":"1","Total":"1
"},{"Year":"2016-11-09","Numb":"1","Total":"1
"},{"Year":"2016-11-10","Numb":"2","Total":"1
"},{"Year":"2016-11-16","Numb":"2","Total":"1
"},{"Year":"2016-11-20","Numb":"1","Total":"1
"},{"Year":"2016-11-22","Numb":"1","Total":"1
"},{"Year":"2016-12-05","Numb":"1","Total":"1
"},{"Year":"2016-12-06","Numb":"1","Total":"1
"},{"Year":"2016-12-07","Numb":"1","Total":"1
"},{"Year":"2016-10-27","Numb":"1","Total":"2
"},{"Year":"2016-10-31","Numb":"1","Total":"2
"},{"Year":"2016-11-02","Numb":"1","Total":"2
"},{"Year":"2016-11-05","Numb":"1","Total":"2
"},{"Year":"2016-11-07","Numb":"1","Total":"2
"},{"Year":"2016-11-08","Numb":"1","Total":"2
"},{"Year":"2016-11-09","Numb":"1","Total":"2
"},{"Year":"2016-11-10","Numb":"2","Total":"2
"},{"Year":"2016-11-16","Numb":"2","Total":"2
"},{"Year":"2016-11-20","Numb":"1","Total":"2
"},{"Year":"2016-11-22","Numb":"1","Total":"2
"},{"Year":"2016-12-05","Numb":"1","Total":"2
"},{"Year":"2016-12-06","Numb":"1","Total":"2
"},{"Year":"2016-12-07","Numb":"1","Total":"2
"},{"Year":"2016-10-27","Numb":"1","Total":"3
"},{"Year":"2016-10-31","Numb":"1","Total":"3
"},{"Year":"2016-11-02","Numb":"1","Total":"3
"},{"Year":"2016-11-05","Numb":"1","Total":"3
"},{"Year":"2016-11-07","Numb":"1","Total":"3
"},{"Year":"2016-11-08","Numb":"1","Total":"3
"},{"Year":"2016-11-09","Numb":"1","Total":"3
"},{"Year":"2016-11-10","Numb":"2","Total":"3
"},{"Year":"2016-11-16","Numb":"2","Total":"3
"},{"Year":"2016-11-20","Numb":"1","Total":"3
"},{"Year":"2016-11-22","Numb":"1","Total":"3
"},{"Year":"2016-12-05","Numb":"1","Total":"3
"},{"Year":"2016-12-06","Numb":"1","Total":"3
"},{"Year":"2016-12-07","Numb":"1","Total":"3
"},{"Year":"2016-10-27","Numb":"1","Total":"4
"},{"Year":"2016-10-31","Numb":"1","Total":"4
"},{"Year":"2016-11-02","Numb":"1","Total":"4
"},{"Year":"2016-11-05","Numb":"1","Total":"4
"},{"Year":"2016-11-07","Numb":"1","Total":"4
"},{"Year":"2016-11-08","Numb":"1","Total":"4
"},{"Year":"2016-11-09","Numb":"1","Total":"4
"},{"Year":"2016-11-10","Numb":"2","Total":"4
"},{"Year":"2016-11-16","Numb":"2","Total":"4
"},{"Year":"2016-11-20","Numb":"1","Total":"4
"},{"Year":"2016-11-22","Numb":"1","Total":"4
"},{"Year":"2016-12-05","Numb":"1","Total":"4
"},{"Year":"2016-12-06","Numb":"1","Total":"4
"},{"Year":"2016-12-07","Numb":"1","Total":"4
"},{"Year":"2016-10-27","Numb":"1","Total":"5
"},{"Year":"2016-10-31","Numb":"1","Total":"5
"},{"Year":"2016-11-02","Numb":"1","Total":"5
"},{"Year":"2016-11-05","Numb":"1","Total":"5
"},{"Year":"2016-11-07","Numb":"1","Total":"5
"},{"Year":"2016-11-08","Numb":"1","Total":"5
"},{"Year":"2016-11-09","Numb":"1","Total":"5
"},{"Year":"2016-11-10","Numb":"2","Total":"5
"},{"Year":"2016-11-16","Numb":"2","Total":"5
"},{"Year":"2016-11-20","Numb":"1","Total":"5
"},{"Year":"2016-11-22","Numb":"1","Total":"5
"},{"Year":"2016-12-05","Numb":"1","Total":"5
"},{"Year":"2016-12-06","Numb":"1","Total":"5
"},{"Year":"2016-12-07","Numb":"1","Total":"5
"},{"Year":"2016-10-27","Numb":"1","Total":"6
"},{"Year":"2016-10-31","Numb":"1","Total":"6
"},{"Year":"2016-11-02","Numb":"1","Total":"6
"},{"Year":"2016-11-05","Numb":"1","Total":"6
"},{"Year":"2016-11-07","Numb":"1","Total":"6
"},{"Year":"2016-11-08","Numb":"1","Total":"6
"},{"Year":"2016-11-09","Numb":"1","Total":"6
"},{"Year":"2016-11-10","Numb":"2","Total":"6
"},{"Year":"2016-11-16","Numb":"2","Total":"6
"},{"Year":"2016-11-20","Numb":"1","Total":"6
"},{"Year":"2016-11-22","Numb":"1","Total":"6
"},{"Year":"2016-12-05","Numb":"1","Total":"6
"},{"Year":"2016-12-06","Numb":"1","Total":"6
"},{"Year":"2016-12-07","Numb":"1","Total":"6
"},{"Year":"2016-10-27","Numb":"1","Total":"7
"},{"Year":"2016-10-31","Numb":"1","Total":"7
"},{"Year":"2016-11-02","Numb":"1","Total":"7
"},{"Year":"2016-11-05","Numb":"1","Total":"7
"},{"Year":"2016-11-07","Numb":"1","Total":"7
"},{"Year":"2016-11-08","Numb":"1","Total":"7
"},{"Year":"2016-11-09","Numb":"1","Total":"7
"},{"Year":"2016-11-10","Numb":"2","Total":"7
"},{"Year":"2016-11-16","Numb":"2","Total":"7
"},{"Year":"2016-11-20","Numb":"1","Total":"7
"},{"Year":"2016-11-22","Numb":"1","Total":"7
"},{"Year":"2016-12-05","Numb":"1","Total":"7
"},{"Year":"2016-12-06","Numb":"1","Total":"7
"},{"Year":"2016-12-07","Numb":"1","Total":"7
"},{"Year":"2016-10-27","Numb":"1","Total":"9
"},{"Year":"2016-10-31","Numb":"1","Total":"9
"},{"Year":"2016-11-02","Numb":"1","Total":"9
"},{"Year":"2016-11-05","Numb":"1","Total":"9
"},{"Year":"2016-11-07","Numb":"1","Total":"9
"},{"Year":"2016-11-08","Numb":"1","Total":"9
"},{"Year":"2016-11-09","Numb":"1","Total":"9
"},{"Year":"2016-11-10","Numb":"2","Total":"9
"},{"Year":"2016-11-16","Numb":"2","Total":"9
"},{"Year":"2016-11-20","Numb":"1","Total":"9
"},{"Year":"2016-11-22","Numb":"1","Total":"9
"},{"Year":"2016-12-05","Numb":"1","Total":"9
"},{"Year":"2016-12-06","Numb":"1","Total":"9
"},{"Year":"2016-12-07","Numb":"1","Total":"9
"},{"Year":"2016-10-27","Numb":"1","Total":"11
"},{"Year":"2016-10-31","Numb":"1","Total":"11
"},{"Year":"2016-11-02","Numb":"1","Total":"11
"},{"Year":"2016-11-05","Numb":"1","Total":"11
"},{"Year":"2016-11-07","Numb":"1","Total":"11
"},{"Year":"2016-11-08","Numb":"1","Total":"11
"},{"Year":"2016-11-09","Numb":"1","Total":"11
"},{"Year":"2016-11-10","Numb":"2","Total":"11
"},{"Year":"2016-11-16","Numb":"2","Total":"11
"},{"Year":"2016-11-20","Numb":"1","Total":"11
"},{"Year":"2016-11-22","Numb":"1","Total":"11
"},{"Year":"2016-12-05","Numb":"1","Total":"11
"},{"Year":"2016-12-06","Numb":"1","Total":"11
"},{"Year":"2016-12-07","Numb":"1","Total":"11
"},{"Year":"2016-10-27","Numb":"1","Total":"12
"},{"Year":"2016-10-31","Numb":"1","Total":"12
"},{"Year":"2016-11-02","Numb":"1","Total":"12
"},{"Year":"2016-11-05","Numb":"1","Total":"12
"},{"Year":"2016-11-07","Numb":"1","Total":"12
"},{"Year":"2016-11-08","Numb":"1","Total":"12
"},{"Year":"2016-11-09","Numb":"1","Total":"12
"},{"Year":"2016-11-10","Numb":"2","Total":"12
"},{"Year":"2016-11-16","Numb":"2","Total":"12
"},{"Year":"2016-11-20","Numb":"1","Total":"12
"},{"Year":"2016-11-22","Numb":"1","Total":"12
"},{"Year":"2016-12-05","Numb":"1","Total":"12
"},{"Year":"2016-12-06","Numb":"1","Total":"12
"},{"Year":"2016-12-07","Numb":"1","Total":"12
"},{"Year":"2016-10-27","Numb":"1","Total":"13
"},{"Year":"2016-10-31","Numb":"1","Total":"13
"},{"Year":"2016-11-02","Numb":"1","Total":"13
"},{"Year":"2016-11-05","Numb":"1","Total":"13
"},{"Year":"2016-11-07","Numb":"1","Total":"13
"},{"Year":"2016-11-08","Numb":"1","Total":"13
"},{"Year":"2016-11-09","Numb":"1","Total":"13
"},{"Year":"2016-11-10","Numb":"2","Total":"13
"},{"Year":"2016-11-16","Numb":"2","Total":"13
"},{"Year":"2016-11-20","Numb":"1","Total":"13
"},{"Year":"2016-11-22","Numb":"1","Total":"13
"},{"Year":"2016-12-05","Numb":"1","Total":"13
"},{"Year":"2016-12-06","Numb":"1","Total":"13
"},{"Year":"2016-12-07","Numb":"1","Total":"13
"},{"Year":"2016-10-27","Numb":"1","Total":"14
"},{"Year":"2016-10-31","Numb":"1","Total":"14
"},{"Year":"2016-11-02","Numb":"1","Total":"14
"},{"Year":"2016-11-05","Numb":"1","Total":"14
"},{"Year":"2016-11-07","Numb":"1","Total":"14
"},{"Year":"2016-11-08","Numb":"1","Total":"14
"},{"Year":"2016-11-09","Numb":"1","Total":"14
"},{"Year":"2016-11-10","Numb":"2","Total":"14
"},{"Year":"2016-11-16","Numb":"2","Total":"14
"},{"Year":"2016-11-20","Numb":"1","Total":"14
"},{"Year":"2016-11-22","Numb":"1","Total":"14
"},{"Year":"2016-12-05","Numb":"1","Total":"14
"},{"Year":"2016-12-06","Numb":"1","Total":"14
"},{"Year":"2016-12-07","Numb":"1","Total":"14
"},{"Year":"2016-10-27","Numb":"1","Total":"15
"},{"Year":"2016-10-31","Numb":"1","Total":"15
"},{"Year":"2016-11-02","Numb":"1","Total":"15
"},{"Year":"2016-11-05","Numb":"1","Total":"15
"},{"Year":"2016-11-07","Numb":"1","Total":"15
"},{"Year":"2016-11-08","Numb":"1","Total":"15
"},{"Year":"2016-11-09","Numb":"1","Total":"15
"},{"Year":"2016-11-10","Numb":"2","Total":"15
"},{"Year":"2016-11-16","Numb":"2","Total":"15
"},{"Year":"2016-11-20","Numb":"1","Total":"15
"},{"Year":"2016-11-22","Numb":"1","Total":"15
"},{"Year":"2016-12-05","Numb":"1","Total":"15
"},{"Year":"2016-12-06","Numb":"1","Total":"15
"},{"Year":"2016-12-07","Numb":"1","Total":"15
"},{"Year":"2016-10-27","Numb":"1","Total":"16
"},{"Year":"2016-10-31","Numb":"1","Total":"16
"},{"Year":"2016-11-02","Numb":"1","Total":"16
"},{"Year":"2016-11-05","Numb":"1","Total":"16
"},{"Year":"2016-11-07","Numb":"1","Total":"16
"},{"Year":"2016-11-08","Numb":"1","Total":"16
"},{"Year":"2016-11-09","Numb":"1","Total":"16
"},{"Year":"2016-11-10","Numb":"2","Total":"16
"},{"Year":"2016-11-16","Numb":"2","Total":"16
"},{"Year":"2016-11-20","Numb":"1","Total":"16
"},{"Year":"2016-11-22","Numb":"1","Total":"16
"},{"Year":"2016-12-05","Numb":"1","Total":"16
"},{"Year":"2016-12-06","Numb":"1","Total":"16
"},{"Year":"2016-12-07","Numb":"1","Total":"16
"}]
Year: Date Member Added
Numb: How many signed up on that particular date
Total: Running total of new signups
Below is what it should look like.. (just tided up formatting)
[{"Year":"2016-10-27","Numb":"1","Total":"1"},
{"Year":"2016-10-27","Numb":"1","Total":"2"},
{"Year":"2016-11-02","Numb":"1","Total":"3"},
{"Year":"2016-11-05","Numb":"1","Total":"4"},
{"Year":"2016-11-07","Numb":"1","Total":"5"},
{"Year":"2016-11-08","Numb":"1","Total":"6"},
{"Year":"2016-11-09","Numb":"1","Total":"7"},
{"Year":"2016-11-10","Numb":"2","Total":"9"},
{"Year":"2016-11-16","Numb":"2","Total":"11"},
{"Year":"2016-11-20","Numb":"1","Total":"12"},
{"Year":"2016-11-22","Numb":"1","Total":"13"},
{"Year":"2016-12-05","Numb":"1","Total":"14"},
{"Year":"2016-12-06","Numb":"1","Total":"15"},
{"Year":"2016-12-07","Numb":"1","Total":"16"}]
Any assistance would be greatly appreciated. This query is beyond my me.
Helps if you Google the right Question... Just combined two into one....
;-(
$myArray = array();
if ($result = $mysqli->query("SELECT DATE_FORMAT(DateAdded, '%Y-%m-%d') AS Year, COUNT(*) AS Numb, #running_sum:=#running_sum + d.count AS Total FROM (SELECT DateAdded, COUNT(*) AS 'count' FROM details WHERE MemberStatus = 'Active' AND PaymentStatus NOT REGEXP 'Not' GROUP BY DateAdded ORDER BY DateAdded) d JOIN (SELECT #running_sum := 0 AS dummy) dummy GROUP BY DATE_FORMAT(DateAdded,'%Y-%m-%d');")) {
while($row = $result->fetch_array(MYSQL_ASSOC)) {
$myArray[] = $row;
}
echo json_encode($myArray);
}

Cannot Get MYSQL DATEADD to Add The Year

This is really puzzling me as DATEADD should work and it isn't and wondered if anyone knew why. Here is my statement:
$r = mysqli_query($con,"SELECT ID, DATEADD(year,1,BEGINDATE) AS NEXTYEAR FROM b_crm_deal");
while($row = mysqli_fetch_array($r))
{
print "".$row['NEXTYEAR']."<br />";
}
This doesn't return anything. If I was to return the BEGINDATE it is:
2015-08-04 00:00:00
I basically want NEXTYEAR to return 2016-08-04 00:00:00. I've tried the different combinations of year, yyyy, yy and nothing is returning.
DATEADD isn't a valid function in MySQL (it's MSSQL), use date_add instead:
DATE_ADD(BEGINDATE, interval 1 year) AS NEXTYEAR
See the manual for more information.
DATEADD looks wrong to me, see https://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_adddate
so try:
$r = mysqli_query($con,"SELECT ID, ADDDATE(BEGINDATE, INTERVAL 1 YEAR) AS NEXTYEAR FROM b_crm_deal");
while($row = mysqli_fetch_array($r))
{
print "".$row['NEXTYEAR']."<br />";
}

mysql group by php concat

I have a table of fixtures in my DB: hometeam, awayteam, date, time
I am currently using mysql_fetch_array and getting the following:
hometeam awayteam date time
--------------------------------------------
Blackburn Wolves 13/08/2011 3:00pm
Arsenal Liverpool 13/08/2011 3:00pm
etc
I want to return:
13/08/2011
---------------------------
Blackburn, Wolves, 3:00pm
Arsenal, Liverpool, 3:00pm
etc
I have tried group by date, but this just returns 1 fixture for that date which is rubbish.
I have also tried group_concat, but the problem with this is I need to modify the items in this group, such as changing the time format from server time to a readable time like above.
you can sort by date and then check in php when the date changes between two rows. something like this:
function h($s) { return htmlspecialchars($s); }
$conn = mysqli_connect(…);
$result = mysqli_query($conn, 'select * from fixtures order by date');
$lastdate = NULL;
while(($row = mysqli_fetch_assoc($result)) !== FALSE) {
if($lastdate != $row['date']) {
// output date divider/header
echo '<h1>',h($row['date']),'</h1>';
$lastdate = $row['date'];
}
echo h($row['hometeam']), ', ', h($row['awayteam']), ', ', h($row['time']);
}