Jump to content

Barand

Moderators
  • Posts

    24,563
  • Joined

  • Last visited

  • Days Won

    822

Everything posted by Barand

  1. Turn error reporting on
  2. should be <?php echo ($show_lastweek['total']); ?>
  3. Yes, answer the question when it's asked Then $dt = new DateTime('last saturday'); $saturday = $dt->format('Y-m-d'); $sql = "SELECT SUM(total) as total FROM invoices WHERE date_created BETWEEN '$saturday' - INTERVAL 6 DAY AND '$saturday' ";
  4. I'd use SELECT SUM(total) as total FROM invoices WHERE date_created > CURDATE() - INTERVAL 7 DAY but the function you were looking for is DATE_ADD() or DATE_SUB() (which is what you want in this instance)
  5. I set up some test data. Running this query SELECT reviewer, COUNT(*) FROM BookAuthor ba INNER JOIN BookReview br ON ba.book = br.book WHERE ba.author = 'ggg' GROUP BY reviewer HAVING COUNT(*) >= 2; +----------+----------+ | reviewer | COUNT(*) | +----------+----------+ | bbb | 3 | | ddd | 2 | | eee | 2 | | m | 2 | | o | 2 | | p | 2 | +----------+----------+ In my data m, o and p are reviewers but not authors and the question asked for authors so I then used a subquery to select only those reviewers that are also authors SELECT reviewer, COUNT(*) FROM BookAuthor ba INNER JOIN BookReview br ON ba.book = br.book INNER JOIN ( SELECT DISTINCT author FROM bookauthor ) as ba2 ON br.reviewer = ba2.author WHERE ba.author = 'ggg' GROUP BY reviewer HAVING COUNT(*) >= 2; +----------+----------+ | reviewer | COUNT(*) | +----------+----------+ | bbb | 3 | | ddd | 2 | | eee | 2 | +----------+----------+ I could also have used SELECT reviewer, COUNT(*) FROM BookAuthor ba INNER JOIN BookReview br ON ba.book = br.book WHERE ba.author = 'ggg' AND br.reviewer IN (SELECT author FROM bookauthor) GROUP BY reviewer HAVING COUNT(*) >= 2; My data
  6. SysproCompanyJ.dbo.InvWarehouse looks more like a MSSQL tablename.
  7. Have you tested any of these on your database? You have that advantage over me
  8. That should give a count of each author's books that have been reviewed, but I don't think that was the question asked.
  9. No. "A Tale of Two Cities" is NOT equal to "Charles Dickens" so why try to join on them
  10. First, define "last week". Is that during the previous calendar week (and is that Sun-Sat or Mon-Sun) or the last 7 days
  11. You have not specified the join condition between BookAuthor and BookReviewer (ie BA.col_x = BR.coly_y) It more efficient to use an explicit join syntax. EG ... FROM BookAuthor BA INNER JOIN BookReview BR ON BA.col_x = BR.col_y ...
  12. You should at least attempt to do your own assignments. I bet you wish you'd been listening now. What have you tried so far?
  13. SELECT c.name, b. bid, b.brand FROM counties c INNER JOIN productbrands b USING (countyid) WHERE c.countyid = $id
  14. Barand

    SQL Aliasing

    you need to use the CONCAT() function to concatenate fields, not +
  15. Then you haven't read the manual properly. Str_replace returns a string with values replaced. You are not picking up the returned value. function other_checkstr($x){ $x = str_replace("Large","",$x); if(strpos($x,'-')==FALSE){ return trim(substr(strrchr($x, ' '), 1 )); }else{ return trim(substr($x,0, strpos($x,'-')),'-'); } }
  16. What have you tried? Have you tried reading the manual? http://uk1.php.net/manual/en/function.str-replace.php
  17. try SELECT i.content_id, i.item_title, i.item_date FROM items i INNER JOIN ( SELECT content_id, MAX(item_date) as item_date FROM items GROUP BY content_id ) as max USING (content_id, item_date)
  18. First thing to do if you want to do date comparisons is use the correct date format yyyy-mm-dd. You cannot correctly compare other formats.
  19. The principle is quite simple http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
  20. I had something like this in mind $n = 56; $numbers = array_fill_keys(range(2,$n),1); $lim = ceil(sqrt($n)); for ($i=2; $i<$lim; $i++) { $k=2; while ($i*$k <= $n) { $numbers[$i*$k++] = 0; } } echo join(', ', array_keys(array_filter($numbers))); //--> 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
  21. Specify a unique key on (Item_id, product)
  22. The model I gave should be feasible. From the examples you gave mysql> SELECT * FROM task; +--------+--------------------+------------+--------------+-----------+ | idtask | task_description | task_value | special_date | task_type | +--------+--------------------+------------+--------------+-----------+ | 1 | Wear correct shoes | 1 | NULL | I | | 2 | Bring equipment | 1 | NULL | I | | 3 | Turn up | 1 | NULL | I | | 4 | Team task 1 | 1 | NULL | T | | 5 | Team task 2 | 1 | NULL | T | | 6 | Team task 3 | 1 | NULL | T | | 7 | Trip to zoo | 1 | 2014-01-02 | S | +--------+--------------------+------------+--------------+-----------+ mysql> SELECT * FROM completed_task; +------------+----------+--------+--------+------------+ | idcomptask | idperson | idteam | idtask | task_date | +------------+----------+--------+--------+------------+ | 1 | 1 | 1 | 3 | 2014-01-01 | | 2 | 1 | 1 | 2 | 2014-01-01 | | 3 | 2 | 1 | 3 | 2014-01-01 | | 4 | 2 | 1 | 1 | 2014-01-01 | | 5 | NULL | 1 | 4 | 2014-01-01 | | 6 | NULL | 1 | 5 | 2014-01-01 | | 7 | NULL | 1 | 6 | 2014-01-01 | | 8 | 3 | 3 | 3 | 2014-01-01 | | 9 | 3 | 3 | 2 | 2014-01-01 | | 10 | 3 | 3 | 1 | 2014-01-01 | | 11 | 3 | 3 | 7 | 2014-01-02 | <-- special task on different day | 12 | 4 | 3 | 3 | 2014-01-01 | | 13 | NULL | 3 | 5 | 2014-01-01 | +------------+----------+--------+--------+------------+ Individual points query SELECT DATE_FORMAT(task_date, '%x %v') as week , tm.teamname , p.firstname , GROUP_CONCAT(t.task_description SEPARATOR ', ') as tasks , SUM(t.task_value) as TotalPoints FROM completed_task ct INNER JOIN task t USING (idtask) INNER JOIN person p USING (idperson) INNER JOIN team tm USING (idteam) GROUP BY week, teamname,firstname ORDER BY TotalPoints DESC; +---------+----------+-----------+-----------------------------------------------------------+-------------+ | week | teamname | firstname | tasks | TotalPoints | +---------+----------+-----------+-----------------------------------------------------------+-------------+ | 2014 01 | Green | Anne | Trip to zoo, Turn up, Wear correct shoes, Bring equipment | 4 | | 2014 01 | Blue | Fred | Bring equipment, Turn up | 2 | | 2014 01 | Blue | John | Turn up, Wear correct shoes | 2 | | 2014 01 | Green | Bob | Turn up | 1 | +---------+----------+-----------+-----------------------------------------------------------+-------------+ Team task query SELECT DATE_FORMAT(task_date, '%x %v') as week , tm.teamname , GROUP_CONCAT(t.task_description SEPARATOR ', ') as tasks , SUM(t.task_value) as TotalPoints FROM completed_task ct INNER JOIN task t USING (idtask) INNER JOIN team tm USING (idteam) WHERE t.task_type = 'T' GROUP BY week, teamname ORDER BY TotalPoints DESC; +---------+----------+---------------------------------------+-------------+ | week | teamname | tasks | TotalPoints | +---------+----------+---------------------------------------+-------------+ | 2014 01 | Blue | Team task 1, Team task 2, Team task 3 | 3 | | 2014 01 | Green | Team task 2 | 1 | +---------+----------+---------------------------------------+-------------+ Overall team scores query SELECT DATE_FORMAT(task_date, '%x %v') as week , tm.teamname , SUM(t.task_value) as TotalPoints FROM completed_task ct INNER JOIN task t USING (idtask) INNER JOIN team tm USING (idteam) GROUP BY week, teamname ORDER BY TotalPoints DESC; +---------+----------+-------------+ | week | teamname | TotalPoints | +---------+----------+-------------+ | 2014 01 | Blue | 7 | | 2014 01 | Green | 6 | +---------+----------+-------------+
  23. Easiest way by far, for you and the user, is to use a datepicker (eg SCW or jQuery datepicker)
  24. or explode? $string = 'Crossover Crop Top In Burgundy, Burgundy'; list($title, $other) = explode(',', $string); echo 'Text before comma: ' . $title. '<br />'; echo 'Text after comma: '. $other;
  25. I wouldn't store month, day and year when they are already part of the stored date. Your bind parameter should be $date_value, not $_POST['date-value'] as that doesn't exist. Where is $_POST['id'] from, I didn't spot it in the form?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.