MySQL Running Total in JSON Output - mysql
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);
}
Related
Convert Sql query to sql CodeIgniter
convert sql query to sql codeigniter i done try to use this method How to convert sql query to codeigniter active records but not working for me... so i try to post in here this my sql query $sql = "SELECT b.id, b.us_id, b.kredit, b.info,u.us_name, u.us_username, u.us_email, u.us_phone, b.cnt, b.amnt FROM users u JOIN (SELECT id, us_id, kredit, info, COUNT(info) cnt, SUM(kredit) amnt FROM balance_history GROUP BY info HAVING cnt > 1 ) AS b ON u.us_id = b.us_id WHERE b.kredit != '0' AND b.info NOT LIKE '[PERBAIKAN]%' AND (b.info LIKE 'Transfer saldo%' OR b.info LIKE 'Ket%') ORDER BY b.id ASC"; thanks before...
If you aren't loaded database globally, you can load it by calling $this->load->database(); The following query may provide the same db call. $this->db->select('id, us_id, kredit, info, COUNT(info) cnt, SUM(kredit) amnt') ->from('balance_history') ->group_by('info') ->having('cnt > 1'); $subquery = $this->db->get_compiled_select(); $query = $this->db ->select('b.id, b.us_id, b.kredit, b.info,u.us_name,u.us_username, u.us_email, u.us_phone, b.cnt, b.amnt') ->from('users u') ->join('('.$subquery.') b','u.us_id = b.us_id') ->where('b.kredit !=','0', true) ->not_like('b.info', '[PERBAIKAN]', 'after', false, true) ->where('(b.info LIKE "Transfer saldo%" OR b.info LIKE "Ket%")') ->get();
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 selecting rows from two seperate tables with one being selected at random
I am currently developing a multiple choice football quiz driven by a back end database. For my database I have created 2 tables. The first table is called questions and contains the following data Id, Team1, Team2, Score1, Score2, Year and Round as the attributes The second table I have is for answers and contains the following data Id and Team Sample Data is shown below Questions: 1,Spain,Holland,0,0,2010,Final 2,England,Germany,1,4,2010,Last 16 Answers 1,England 1,France 1,Germany 1,Brazil 1,Spain 1,Holland I have kept the Id of all teams as 1 so that they can all be in the same answer set when called The purpose of my application is to ask a question to the user using a template as shown below $thisQuestion = 'Which team defeated '. $team1.' '.$score1.' - '.$score2.' in the '. $round .' of the '.$year.' world cup'; Below the question I wish to have 4 teams shown to the user as possible answers with 3 being drawn randomly from the answers table and the 4th being the correct answer. So far I have only been able to call 3 random teams from the answer table using the following code $sql2 = mysql_query("SELECT * FROM answers2 WHERE id=1 ORDER BY RAND() LIMIT 3"); Any help would be appreciated The full sample code for the page is shown below <?php session_start(); require_once("scripts/connect_db.php"); $countArray = ""; if(isset($_GET['question'])){ $question = preg_replace('/[^0-9]/', "", $_GET['question']); $outputText = ""; $answerText = ""; $r = ""; $sql = mysql_query("SELECT id FROM questions"); $numberOfQuestions = mysql_num_rows($sql); if(!isset($_SESSION['a_array']) || $_SESSION['a_array'] < 1){ $currQuestion = "1"; }else{ $countArray = count($_SESSION['a_array']); } if($countArray > $numberOfQuestions){ unset($_SESSION['a_array']); header("location: menu.php"); exit(); } if($countArray >= $numberOfQuestions){ echo '<p>There are no more questions. Please enter your name and click next to find out your score</p> <form action="userResults.php" method="post"> <input type="hidden" name="complete" value="true"> <input type="text" name="username"> <input type="submit" value="Finish"> </form>'; $outputText = ''.$r.','.$answerText.', '; echo $outputText; exit(); } $singleSQL = mysql_query("SELECT * FROM questions WHERE id='$question' LIMIT 1"); while($row = mysql_fetch_array($singleSQL)){ $id = $row['ID']; $team1 = $row['Team1']; $team2 = $row['Team2']; $score1 = $row['Score1']; $score2 = $row['Score2']; $round = $row['Round']; $year= $row['Year']; $thisQuestion = 'Which team defeated '. $team1.' '.$score1.' - '.$score2.' in the '. $round .' of the '.$year.' world cup'; $question_id = $row['ID']; $r = '<h2>'.$thisQuestion.'</h2>'; $sql2 = mysql_query("SELECT * FROM answers2 WHERE id=1 ORDER BY RAND() LIMIT 3"); while($row2 = mysql_fetch_array($sql2)){ $answer = $row2['team']; $correctAnswer = $team2; $answerText .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correctAnswer.'">'.$answer.'</label> <input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br /> '; } $outputText = ''.$r.','.$answerText.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span><br />'; echo $outputText; } } ?>
This is not a complete answer but I think you can easily expand it to do what you want. The main problem (as I understand it) is this: you need a list of four team names randomly selected except it must include one specific team name and with all 4 arranged randomly. It turns out to be fairly simple in MySQL. Here is a simple table containing just the team names for illustration. CREATE TABLE Teams ( Team VARCHAR( 20 ) ); insert into Teams( Team ) SELECT 'Spain' UNION ALL SELECT 'Holland' UNION ALL SELECT 'England' UNION ALL SELECT 'Germany' UNION ALL SELECT 'Brazil' UNION ALL SELECT 'Mexico' UNION ALL SELECT 'Uraguay' UNION ALL SELECT 'Sweden' UNION ALL SELECT 'France'; Then get Team1 from your question. Here I've just set it to a value. set #Team1 := 'Germany'; select Team from( SELECT Team FROM Teams WHERE Team <> #Team1 ORDER BY Rand() limit 3 ) T union all SELECT #Team1 order by Rand(); This produces four different names each time you execute it, except that the list will always include Germany. You might consider changing the last line to order by Team as a random list of names will still be random if arranged in alphabetical order.
display 10 percent of all items mysql
I'm just a beginner at mysql so in school we got task to do. It goes like this. Display / print 10% of all books from books in falling order. So i tried to use limit, but it doesn't work. What can i do? My code i've tried to use: select title, price from book order by price desc limit (select count(*)*0.1 from book); thank you for your answers!
limit values have to be hard-coded constants. You can't use variables on them, e.g. select ... limit #somevar is a syntax error. You also can't use sub-queries or other dynamic values either. So you're stuck with either fetching the row count ahead of time and stuff it into the query string as a "hard-coded" value: $ten_percent = get_from_database('select count(*) / 10 from book'); $sql = "SELECT .... LIMIT $ten_percent"; Or you simply fetch everything and then abort your loop once you've reached 10%: $sql = "SELECT ...."; $result = mysql_query($sql) or die(mysql_error()); $total_rows = mysql_num_rows($result); $fetched = 0; while($row = mysql_fetch_assoc()) { $fetched++; if ($fetched >= ($total_rows / 10)) { break; // abort the loop at 10% } ... do stuff with $row }
php mysql group by date with yyyy-mm-dd format
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;