-
Posts
24,573 -
Joined
-
Last visited
-
Days Won
824
Everything posted by Barand
-
Displaying calculated averages for all football matches from fixtures list
Barand replied to DariusB's topic in MySQL Help
I had to change CURDATE() in the final WHERE clause (no Sunday matches) mysql> SELECT ko_time -> , team -> , AVG(score) -> FROM -> ( -> SELECT score -> , @seq:=IF(team<>@prev,1,@seq+1) as seq -> , @prev:=team as team -> FROM -> ( -> SELECT -> 'H' as venue -> , match_date -> , home as team -> , home_score as score -> FROM championship -> WHERE match_date < CURDATE() -> UNION ALL -> SELECT -> 'A' as venue -> , match_date -> , away as team -> , away_score as score -> FROM championship -> WHERE match_date < CURDATE() -> ORDER BY team, match_date DESC -> ) scores -> JOIN (SELECT @prev:='', @seq=0) init -> ) av -> INNER JOIN -> championship c ON av.team IN (c.home, c.away) -> WHERE c.match_date = '2015-09-19' AND seq < 6 -> GROUP BY team; +---------+--------------------+------------+ | ko_time | team | AVG(score) | +---------+--------------------+------------+ | 15:00 | Blackburn | 0.6000 | | 15:00 | Bolton | 0.6000 | | 15:00 | Brentford | 0.6000 | | 15:00 | Brighton | 1.4000 | | 15:00 | Bristol City | 1.0000 | | 15:00 | Cardiff | 1.2000 | | 15:00 | Charlton | 1.0000 | | 15:00 | Fulham | 1.4000 | | 15:00 | Huddersfield | 0.6000 | | 15:00 | Hull City | 1.0000 | | 15:00 | Leeds | 0.8000 | | 12:30 | Middlesbrough | 1.6000 | | 15:00 | Milton Keynes Dons | 0.2000 | | 12:30 | Nottingham | 1.0000 | | 15:00 | Preston | 0.6000 | | 15:00 | QPR | 1.6000 | | 15:00 | Reading | 1.6000 | | 15:00 | Rotherham | 1.0000 | | 15:00 | Sheffield Wed | 0.6000 | | 15:00 | Wolves | 1.0000 | +---------+--------------------+------------+ 20 rows in set (0.02 sec) -
Displaying calculated averages for all football matches from fixtures list
Barand replied to DariusB's topic in MySQL Help
try SELECT ko_time , team , AVG(score) FROM ( SELECT score , @seq:=IF(team<>@prev,1,@seq+1) as seq , @prev:=team as team FROM ( SELECT 'H' as venue , match_date , home as team , home_score as score FROM championship WHERE match_date < CURDATE() UNION ALL SELECT 'A' as venue , match_date , away as team , away_score as score FROM championship WHERE match_date < CURDATE() ORDER BY team, match_date DESC ) scores JOIN (SELECT @prev:='', @seq=0) init ) av INNER JOIN championship c ON av.team IN (c.home, c.away) WHERE c.match_date = curdate() AND seq < 6 GROUP BY team; -
Sorry I couldn't be more helpful but I wasn't going to attempt to juggle data from five tables of unknown structure and content in my head.
-
Don't know, I can't see the data from here.
-
You can use "WITH ROLLUP" to get the overall totals. Add it at the end of the quey . . . . . . GROUP BY date WITH ROLLUP gives +------------+---------+--------+-----------+-----------+-------+ | date | created | offers | contracts | purchases | sales | +------------+---------+--------+-----------+-----------+-------+ | 2015-09-01 | 1 | 0 | 0 | 0 | 0 | | 2015-09-02 | 1 | 2 | 0 | 0 | 0 | | 2015-09-03 | 1 | 0 | 1 | 0 | 0 | | 2015-09-04 | 1 | 2 | 3 | 0 | 0 | | 2015-09-05 | 1 | 1 | 1 | 0 | 0 | | 2015-09-12 | 0 | 0 | 0 | 1 | 0 | | 2015-09-18 | 0 | 0 | 0 | 0 | 1 | | 2015-10-16 | 0 | 0 | 0 | 1 | 0 | | 2015-10-26 | 0 | 0 | 0 | 1 | 0 | | 2015-11-06 | 0 | 0 | 0 | 1 | 0 | | 2015-12-01 | 0 | 0 | 0 | 1 | 0 | | 2015-12-15 | 0 | 0 | 0 | 0 | 1 | | 2015-12-30 | 0 | 0 | 0 | 0 | 1 | | 2016-01-15 | 0 | 0 | 0 | 0 | 1 | | 2016-02-01 | 0 | 0 | 0 | 0 | 1 | | NULL | 5 | 5 | 5 | 5 | 5 | +------------+---------+--------+-----------+-----------+-------+ Back at the original insertion of the data. That table has more of a spreadsheet flavour than a db table. Is it created by collecting dates from other tables?
-
this is my query with my data SELECT date , SUM(IF(type='CR',1,0)) as created , SUM(IF(type='OF',1,0)) as offers , SUM(IF(type='CO',1,0)) as contracts , SUM(IF(type='PU',1,0)) as purchases , SUM(IF(type='SA',1,0)) as sales FROM ( SELECT 'CR' as type, created as date FROM property UNION ALL SELECT 'OF' as type, offer_date as date FROM property UNION ALL SELECT 'CO' as type, contract_date as date FROM property UNION ALL SELECT 'PU' as type, purchase_date as date FROM property UNION ALL SELECT 'SA' as type, sale_date as date FROM property ) props GROUP BY date; +------------+---------+--------+-----------+-----------+-------+ | date | created | offers | contracts | purchases | sales | +------------+---------+--------+-----------+-----------+-------+ | 2015-09-01 | 1 | 0 | 0 | 0 | 0 | | 2015-09-02 | 1 | 2 | 0 | 0 | 0 | | 2015-09-03 | 1 | 0 | 1 | 0 | 0 | | 2015-09-04 | 1 | 2 | 3 | 0 | 0 | | 2015-09-05 | 1 | 1 | 1 | 0 | 0 | | 2015-09-12 | 0 | 0 | 0 | 1 | 0 | | 2015-09-18 | 0 | 0 | 0 | 0 | 1 | | 2015-10-16 | 0 | 0 | 0 | 1 | 0 | | 2015-10-26 | 0 | 0 | 0 | 1 | 0 | | 2015-11-06 | 0 | 0 | 0 | 1 | 0 | | 2015-12-01 | 0 | 0 | 0 | 1 | 0 | | 2015-12-15 | 0 | 0 | 0 | 0 | 1 | | 2015-12-30 | 0 | 0 | 0 | 0 | 1 | | 2016-01-15 | 0 | 0 | 0 | 0 | 1 | | 2016-02-01 | 0 | 0 | 0 | 0 | 1 | +------------+---------+--------+-----------+-----------+-------+
-
I am working on the query for, but, in the meantime I would advise you use DATE, DATETIME or TIMESTAMP type columns for date/times. Not only are these immediately useful without conversion to a usable format, but you can tell what the the date is when you view your data, instead of an unintelligible string of digits So while I am testing I am using DATE columns and inserted this data (to preserve your dates) INSERT INTO `property` (`id`, `created`, `offer_date`, `contract_date`, `purchase_date`, `sale_date`) VALUES (1, FROM_UNIXTIME(1441065600), FROM_UNIXTIME(1441152000), FROM_UNIXTIME(1441238400), FROM_UNIXTIME(1442016000), FROM_UNIXTIME(1442534400)), (2, FROM_UNIXTIME(1441152000), FROM_UNIXTIME(1441152000), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1445817600), FROM_UNIXTIME(1450137600)), (3, FROM_UNIXTIME(1441238400), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1444953600), FROM_UNIXTIME(1451433600)), (4, FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1441324800), FROM_UNIXTIME(1446768000), FROM_UNIXTIME(1452816000)), (5, FROM_UNIXTIME(1441411200), FROM_UNIXTIME(1441411200), FROM_UNIXTIME(1441411200), FROM_UNIXTIME(1448928000), FROM_UNIXTIME(1454284800)); giving a readable +----+------------+------------+---------------+---------------+------------+ | id | offer_date | created | contract_date | purchase_date | sale_date | +----+------------+------------+---------------+---------------+------------+ | 1 | 2015-09-02 | 2015-09-01 | 2015-09-03 | 2015-09-12 | 2015-09-18 | | 2 | 2015-09-02 | 2015-09-02 | 2015-09-04 | 2015-10-26 | 2015-12-15 | | 3 | 2015-09-04 | 2015-09-03 | 2015-09-04 | 2015-10-16 | 2015-12-30 | | 4 | 2015-09-04 | 2015-09-04 | 2015-09-04 | 2015-11-06 | 2016-01-15 | | 5 | 2015-09-05 | 2015-09-05 | 2015-09-05 | 2015-12-01 | 2016-02-01 | +----+------------+------------+---------------+---------------+------------+
-
Those need to be variables ($ prefixes) or you need to have defined them as constants of course you can. I didn't because I wanted to show a mysqli connection I normally use an included file in which I define HOST, USERNAME, PASSWORD and DATABASE
-
It would help if we knew what the data looks like. Table structure?
-
Monitor folder on webserver for new files with PHP to RSS feed
Barand replied to SavaSavanovic's topic in PHP Coding Help
I notice you changed return filemtime( $b ) - filemtime( $a ); to return filemtime( $a ) - filemtime( $b ); That will now give you the 5 oldest files and not the 5 latest. This will retrieve filenames from the folder and all subfolder $dir = 'path/to/folder'; // set start directory $show = 5; $files = array(); // array to store retrieved filenames dirList($dir, $files); // call the recursive function to get all files usort( $files, create_function('$a, $b', 'return filemtime( $b ) - filemtime( $a );') ); for ( $i = 0; $i < $show; ++$i ) echo '<a href="', $file = $files[$i], '">', ucfirst( strtolower(substr($file, 0, strpos($file, '.'))) ), '</a> - ', date( 'D, d M y H:i:s', filemtime($file) ), '<br />', "\n"; /***************************************************************** ** function to retrieve files from folder and all subfolders ******************************************************************/ function dirList($dir, &$results, $level=0) { $files = glob($dir.'/*'); foreach ($files as $f) { if (is_dir($f)) continue; $results[] = $f; } $files = glob($dir.'/*', GLOB_ONLYDIR); foreach ($files as $f) { if (is_dir($f)) { dirList($f, $results, $level+1); } } } -
Sounds like this is the model that are describing +--------------+ | parent | +--------------+ | parent_id |-----+ | parentname | | +--------------+ | +-------------+ | | child | | +-------------+ | | child_id | | | childname | +-------<| parent_id | +-------------+ in which case JOIN using the parent id column SELECT p.parent_id , p.parentname , c.childname FROM parent p LEFT JOIN child c ON p.parent_id = c.parent_id ORDER BY parentname Loop through the results outputting the parent name only when it changes to new value.
-
Use the big "Start New Topic" button at the top right of the forum page
-
Instead of just hijacking this thread, why don't you read it? You just might find the answer you are looking for.
-
Most of the cities in that sample begin with a space (or other non-printing character) and a "\n" In the example I picked out the length is 9 when it should be only 7. You need to trim() the elements in your data
-
... and is the file being opened? Check the value of $fil after opening to make sure it isn't = false.
-
foreach ($myarray as $yr => $yrdata) { echo $yr . '<br>'; foreach ($yrdata as $doc) { echo $doc['documentname'] . '<br>'; } }
-
execute the query and list the results. (I have use mysqli - which you should be using (or PDO) and not the deprecated mysql_ library) $mysqli = new mysqli(HOST,USERNAME,PASSWORD,'bikes'); $sql = "SELECT m.modell , m.antal , o.total , ROUND(o.total/m.antal*100, 1) as pc FROM modeller m LEFT JOIN ( SELECT mc as modell , COUNT(*) as total FROM owner GROUP BY modell ) o USING (modell) ORDER BY modell"; $res = $mysqli->query($sql); // execute the query while (list($model, $antal, $total, $pc) = $res->fetch_row()) { // list the results echo "We have $total pcs $model ( {$pc}% ) of produced $antal pcs in the database<br>"; }
-
this will give all models and their % in the database SELECT m.modell , m.antal , o.total , ROUND(o.total/m.antal*100, 1) as pc FROM modeller m LEFT JOIN ( SELECT mc as modell , COUNT(*) as total FROM owner GROUP BY modell ) o USING (modell) ORDER BY modell sample output +--------+-------+-------+------+ | modell | antal | total | pc | +--------+-------+-------+------+ | A50 | 20 | NULL | NULL | | B25 | 50 | 6 | 12.0 | | B40 | 50 | 6 | 12.0 | | B50 | 100 | 4 | 4.0 | | VH500 | 20 | 6 | 30.0 | +--------+-------+-------+------+
-
Monitor folder on webserver for new files with PHP to RSS feed
Barand replied to SavaSavanovic's topic in PHP Coding Help
Here's a function to return the N latest files in a folder $dir = "path/to/folder"; $latest = latestFiles($dir, 5); echo '<pre>',print_r($latest, true),'</pre>'; // view results function latestFiles($dir, $n) { $files = glob($dir.'/*.*'); // sort files by date DESC usort($files, function($a,$b) {return filemtime($b) - filemtime($a);}); // return latest n files return array_slice($files,0,$n); } -
I give up.
-
Also you are creating a lot of extra work for yourself At the moment you Select names from owners. User selects a name On submitting formselect o_id from owners where the name matches use o_id in the insert you should Select o_id and name from from owners set the id as the option value so user selects id Now when you process the form you have the o_id in the POST data and you don't need the extra query. You can do the insert straight away. (The same goes for the property)
-
then the query is still failing. try $result1 = mysqli_query($conn , $sql2); if ($result==false) { die ($conn->error); } ans see what the error is
-
A symptom that your query failed. Probably because $_POST['name'] is a string and therefore should be in single quotes SELECT Owner_ID FROM owners WHERE name = '$_POST[name]'
-
You are not reading the reply $own and $pro are not variables containing the ids, they contain query result objects. As secweb said, you need to fetch the row from the results and use the id value from the row. .
-
Here's a basic recursive glob() script $dir = 'c:/inetpub/wwwroot/'; // set start directory dirList($dir); // call the recursive function function dirList($dir, $level=0) { $files = glob($dir.'/*'); foreach ($files as $f) { if (is_dir($f)) continue; echo str_repeat('|---- ', $level) . basename($f) . '<br>'; } $files = glob($dir.'/*', GLOB_ONLYDIR); foreach ($files as $f) { if (is_dir($f)) { echo str_repeat('|---- ', $level) . "<strong>$f</strong><br>"; dirList($f, $level+1); } } }