Jump to content

Barand

Moderators
  • Posts

    24,551
  • Joined

  • Last visited

  • Days Won

    821

Community Answers

  1. Barand's post in CONCAT was marked as the answer   
    You would put it in the SELECT clause. Also note you cannot use aliases in WHERE clauses
    SELECT groups.grid , groups.code1 , products.codeDX , products.CodeSX , products.prodid , products.groupid , CONCAT (code1, ".", codeDX) AS op1 , CONCAT (code1, ".", codeSX) AS op2 FROM groups LEFT JOIN products ON groups.grid = products.groupid WHERE (CONCAT(code1, ".", codeDX) LIKE '%$trimm%'     OR CONCAT(code1, ".", codeSX) LIKE '%$trimm%' ) ORDER BY groups.grid
  2. Barand's post in Graphing number of people from two times was marked as the answer   
    Set up the array structure that you need for your chart data then process your input, accumulating the data for each hour
    // // INPUT DATA // $input = [ ['0900', '2000', 1, 5], ['1000', '2200', 1, 3] ]; // // PREPARE ARRAYS TO STORE CHART DATA // $init = array_fill_keys(range(0,23), 0); $data = [ 0 => ['name'=>'Supervisors', 'data'=>$init ], 1 => ['name'=>'Staff', 'data'=>$init ] ]; // // PROCESS THE INPUT // foreach ($input as $row) { list($start, $end, $sup, $staff) = $row; for ($hr=$start/100; $hr<$end/100; $hr++) { $data[0]['data'][$hr]+=$sup; $data[1]['data'][$hr]+=$staff; } } // // CHECK CHART DATA // echo '<pre>',print_r($data, true),'</pre>'; result
    Array ( [0] => Array ( [name] => Supervisors [data] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 1 [10] => 2 [11] => 2 [12] => 2 [13] => 2 [14] => 2 [15] => 2 [16] => 2 [17] => 2 [18] => 2 [19] => 2 [20] => 1 [21] => 1 [22] => 0 [23] => 0 ) ) [1] => Array ( [name] => Staff [data] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 0 [5] => 0 [6] => 0 [7] => 0 [8] => 0 [9] => 5 [10] => 8 [11] => 8 [12] => 8 [13] => 8 [14] => 8 [15] => 8 [16] => 8 [17] => 8 [18] => 8 [19] => 8 [20] => 3 [21] => 3 [22] => 0 [23] => 0 ) ) )
  3. Barand's post in change date after midnight was marked as the answer   
    here's one way
    $arr = [ '2016-02-24 16:55', '2016-02-24 17:55', '2016-02-24 19:55', '2016-02-24 23:55', '2016-02-24 00:35', '2016-02-24 01:34' ]; for ($i=1, $j=0, $k=count($arr); $i<$k; $i++, $j++) { $dj = new DateTime($arr[$j]); $di = new DateTime($arr[$i]); if ($di < $dj) { $arr[$i] = $di->modify('+1 days')->format('Y-m-d H:i'); } } echo '<pre>',print_r($arr, true),'</pre>'; /* RESULT ************* Array ( [0] => 2016-02-24 16:55 [1] => 2016-02-24 17:55 [2] => 2016-02-24 19:55 [3] => 2016-02-24 23:55 [4] => 2016-02-25 00:35 [5] => 2016-02-25 01:34 ) ****************************/
  4. Barand's post in Query help was marked as the answer   
    Replace the AND in the ORDER BY with a comma
    ...ORDER BY t2.`turma` ,  t1.`nome` ASC
  5. Barand's post in Accessing complete table data with inner join was marked as the answer   
    If you can't work out that my name on my posts is in exactly the same place that your name is on your posts, then I am not sure you are ready for this.
     
    Anyway, your table should look like this.
    no | seq | s_line_no -----+--------+------------ 32 | 1 | 315 34 | 1 | 12R 34 | 2 | 12R 34 | 3 | 12R 35 | 1 | 12R 37 | 1 | Other If the user enters more than one value then you write more than one row.
     
    Now all you need is a count of the rows where s_line_no = 12R to get the answer of 4.
  6. Barand's post in Selecting count and other info was marked as the answer   
    Don't know what your column names are but something along these lines
    SELECT id, COUNT(*) as tot FROM tablename GROUP BY id
  7. Barand's post in Math God needed was marked as the answer   
    If you want them spaced around the circumference then the attached diagram will tell you what you need to know

  8. Barand's post in database new tables was marked as the answer   
    Would this meet your requirements?

  9. Barand's post in Implementing new table was marked as the answer   
    From what you have said, a lesson can have many classes and each class has many lessons. You therefore have a many-to-many relationship between class and lesson. This would be resolved by using another table to link the two. In this case I suggest it could be a "timetable" table which schedules when each class has a lesson. (See attached model)
     
    Given a class it is then easy to find the students in that class.
     
    Following the links in the diagram you can find which lessons a student has.

  10. Barand's post in How do I use MySQL Quarter() function was marked as the answer   
    my data
    mysql> SELECT * FROM history; +-----------+----------+--------------+-------------+ | historyid | memberid | last_payment | amount_paid | +-----------+----------+--------------+-------------+ | 1 | 1 | 2015-12-20 | 1000.00 | | 2 | 1 | 2016-01-01 | 150.00 | | 3 | 1 | 2016-01-10 | 50.00 | | 4 | 1 | 2016-02-03 | 60.00 | | 5 | 1 | 2016-02-25 | 40.00 | | 6 | 1 | 2016-05-30 | 300.00 | | 7 | 1 | 2016-06-22 | 200.00 | | 8 | 2 | 2016-01-05 | 155.00 | | 9 | 2 | 2016-02-14 | 205.00 | +-----------+----------+--------------+-------------+ mysql> SELECT * FROM quarters; +-----+ | qtr | +-----+ | 1 | | 2 | | 3 | | 4 | +-----+ the query and results - I told you to put the conditions on the history table into the ON clause, not in the WHERE clause.
    mysql> SELECT qtr AS Quarter, SUM(amount_paid) as Total -> FROM quarters q -> LEFT JOIN history h ON qtr = QUARTER(last_payment) -> AND YEAR(last_payment) = YEAR(CURDATE()) -> AND memberid = '1' -> GROUP BY qtr; +---------+--------+ | Quarter | Total | +---------+--------+ | 1 | 300.00 | | 2 | 500.00 | | 3 | NULL | | 4 | NULL | +---------+--------+ If you have them in the WHERE clause then the LEFT JOIN behaves as as an INNER JOIN like this
    mysql> SELECT qtr AS Quarter, SUM(amount_paid) as Total -> FROM quarters q -> LEFT JOIN history h ON qtr = QUARTER(last_payment) -> WHERE YEAR(last_payment) = YEAR(CURDATE()) -> AND memberid = '1' -> GROUP BY qtr; +---------+--------+ | Quarter | Total | +---------+--------+ | 1 | 300.00 | | 2 | 500.00 | +---------+--------+
  11. Barand's post in Converting date format was marked as the answer   
    You can use sql's STR_TO_DATE() function. Concatenate "-01" to the end first so you don't get day 0.
     
    Example
    CREATE TABLE `test_date` ( `id` int(11) NOT NULL AUTO_INCREMENT, `str_date` varchar(20) DEFAULT NULL, `date` date DEFAULT NULL, PRIMARY KEY (`id`) ) mysql> SELECT * FROM test_date; +----+----------+------+ | id | str_date | date | +----+----------+------+ | 1 | 16-Jan | NULL | | 2 | 16-Feb | NULL | | 3 | 16-Mar | NULL | +----+----------+------+ UPDATE test_date SET date = STR_TO_DATE(CONCAT(str_date, '-01'), '%y-%b-%d'); mysql> SELECT * FROM test_date; +----+----------+------------+ | id | str_date | date | +----+----------+------------+ | 1 | 16-Jan | 2016-01-01 | | 2 | 16-Feb | 2016-02-01 | | 3 | 16-Mar | 2016-03-01 | +----+----------+------------+
  12. Barand's post in Mysql query help was marked as the answer   
    Use AND instead of OR.
     
    Alternatively,
    ... WHERE idsala NOT IN (23,24,39)
  13. Barand's post in Join Tables Part 2 was marked as the answer   
    Sorry, I copied and edited the wrong query
    SELECT c.name AS county_name , c.id AS county_id , c.population , group_concat(DISTINCT r.name separator '<br /> ') AS recruiter , group_concat(DISTINCT u.name separator '<br /> ') AS u_name FROM counties c INNER JOIN states s ON c.state_id = s.id LEFT JOIN recruiters r ON r.county_id = c.id LEFT JOIN universities u ON c.id = u.county_id WHERE c.state_id = $StateID GROUP BY c.name
  14. Barand's post in Grouping / Joining / Ordering was marked as the answer   
    What is the group table for when you have the region parent in the region table?
     
    Try
    SELECT reg_name as region , region_id , parent_id FROM clc_crz_regions WHERE parent_id=0 UNION SELECT r1.reg_name , r2.region_id , r1.parent_id FROM clc_crz_regions r1 INNER JOIN clc_crz_regions r2 ON r1.parent_id = r2.region_id WHERE r2.parent_id=0 ORDER BY region_id, parent_id, region +--------------------+-----------+-----------+ | region | region_id | parent_id | +--------------------+-----------+-----------+ | Caribbean | 1 | 0 | | Eastern Caribbean | 1 | 1 | | Southern Caribbean | 1 | 1 | | Western Caribbean | 1 | 1 | | South America | 4 | 0 | | Amazon River | 4 | 4 | +--------------------+-----------+-----------+
  15. Barand's post in splitting a serialized array was marked as the answer   
    unserializing once gave you a serialized string which you then unserialized to get the array.
     
    Looks like your original was serialized twice.
  16. Barand's post in results into array was marked as the answer   
    Do you mean
    while($row = $result->fetch_assoc()){ $events[] = array( 'id' => $row['id'], 'title' => $row['event'], 'start' => $row['eventDate'] ); }
  17. Barand's post in Join Tables Help was marked as the answer   
    My data
    mysql> select * from states; +----+-------------+------------+ | id | name | capital | +----+-------------+------------+ | 1 | Connecticut | Hartford | | 2 | Maine | Augusta | | 3 | Vermont | Montpelier | +----+-------------+------------+ mysql> select * from counties; +----+----------+------------+------------+ | id | state_id | name | population | +----+----------+------------+------------+ | 1 | 3 | Bennington | 37125 | | 2 | 3 | Addison | 36821 | | 3 | 3 | Caledonia | 31227 | | 4 | 1 | Fairfield | 916829 | | 5 | 1 | Hartford | 894014 | | 6 | 2 | Aroostook | 71870 | | 7 | 2 | Cumberland | 281674 | +----+----------+------------+------------+ mysql> select * from recruiters; +----+-----------+---------+ | id | county_id | name | +----+-----------+---------+ | 1 | 1 | Anne | | 2 | 1 | Bernard | | 3 | 2 | Charlie | | 4 | 2 | Dianne | | 5 | 3 | Emma | | 6 | 4 | Fred | | 7 | 4 | George | | 8 | 6 | Henry | | 9 | 6 | Ian | | 10 | 6 | Jane | +----+-----------+---------+ query
    SELECT c.name , c.population , group_concat(r.name separator ', ') as recruiters FROM counties c INNER JOIN states s ON c.state_id = s.id LEFT JOIN recruiters r ON r.county_id = c.id WHERE c.state_id = 2 GROUP BY c.name results
    +------------+------------+------------------+ | name | population | recruiters | +------------+------------+------------------+ | Aroostook | 71870 | Henry, Ian, Jane | | Cumberland | 281674 | NULL | +------------+------------+------------------+
  18. Barand's post in Need help with this json_decode result was marked as the answer   
    echo $Arr['scan_results']['scan_details']['F-prot']['scan_result_i'];
  19. Barand's post in Simple Loop into array was marked as the answer   
    Supposing you have, this (where you said some could be empty)
    $_POST['item_meta'][124] = 'aaa'; $_POST['item_meta'][125] = 'bbb'; $_POST['item_meta'][126] = 'ccc'; $_POST['item_meta'][127] = 'ddd'; $_POST['item_meta'][128] = 'eee'; $_POST['item_meta'][129] = ''; $_POST['item_meta'][130] = 'fff'; $_POST['item_meta'][131] = ''; $_POST['item_meta'][132] = ''; $_POST['item_meta'][133] = 'ggg'; then to get the $meta_value, all you need is
    $meta_value = join(',', array_filter($_POST['item_meta'])); to check
    echo $meta_value; //--> aaa,bbb,ccc,ddd,eee,fff,ggg
  20. Barand's post in updating table query was marked as the answer   
    try
    UPDATE ballot INNER JOIN ( SELECT username , COUNT(neptune) as ncount FROM ballot GROUP BY username HAVING ncount > 5 ) n USING (username) SET morethan5 = 'yes' However, it is bad practice to store derived data like that in a db table.
  21. Barand's post in PHP Questionnaires Setup and structure was marked as the answer   
    FooKelvin,
     
    I have attached a data model to get you started. You should employ this same approach to your problem your other topic at
    http://forums.phpfreaks.com/topic/300461-generate-report-from-check-box-value-comments/?do=findComment&comment=1529890

  22. Barand's post in formatting php count results was marked as the answer   
    There is definitely something going on. If I create a test table your code is  fine
    mysql> SELECT id, username FROM ballot; +----+--------------+ | id | username | +----+--------------+ | 1 | habsfan4life | | 2 | demo | | 3 | habs4stanley | | 4 | habsfan4life | | 5 | demo | +----+--------------+ 5 rows in set (0.00 sec) mysql> SELECT username, COUNT(*) -> FROM ballot -> GROUP BY username; +--------------+----------+ | username | COUNT(*) | +--------------+----------+ | demo | 2 | | habs4stanley | 1 | | habsfan4life | 2 | +--------------+----------+ 3 rows in set (0.00 sec)
  23. Barand's post in Beginner needs help.... was marked as the answer   
    Select the data you want to output with a WHERE clause
    SELECT dt, people, c_fname, c_lname, c_phone, c_notes, code FROM restaurantbooking_bookings WHERE dt BETWEEN CURDATE() AND CURDATE() + INTERVAL 3 DAY ORDER BY dt
  24. Barand's post in Get PHP Variables with Ajax or Jquery was marked as the answer   
    Easiest way to get the form fields is to use serialize(). Try this example
     
    testing.html
    <html> <head> <title>Example</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script type='text/javascript'> $().ready(function() { $('#BtnInserisci').click(function() { var formdata = $("#form1").serialize(); $.post( "testing.php", formdata, function(html) { $('#BtnInserisci').before(html); }, 'html' ) }) }) </script> </head> <body> <form id='form1'> Field 1: <input type='text' name='field1' value='aaaa'><br> Field 2: <input type='text' name='field2' value='bbbb'><br> Field 3: <input type='text' name='field3' value='cccc'><br> <input type='button' name='BtnInserisci' id='BtnInserisci' value='Test'> </form> </body> </html> testing.php
    <?php $html = ''; if ($_SERVER['REQUEST_METHOD']=='POST') { $html = '<div style="width: 200px; border: 1px solid gray; margin:10px;padding:5px">'; foreach ($_POST as $k => $v) { $html .= "<p>$k : $v</p>"; } $html .= "</div>\n"; } echo $html; ?>
  25. Barand's post in WEEKLY QUERY was marked as the answer   
    I think it's fixed. Try
    SELECT s.week , CONCAT(DATE_FORMAT(@day1 + INTERVAL (s.week-1)*7 DAY, '%e %b'),' - ', DATE_FORMAT(@day1 + INTERVAL (s.week-1)*7 + 6 DAY, '%e %b')) as wkcomm , @slab:=@slab+ifnull(slabcount,0)-@driedcf as slab_count , @dried:=@dried+ifnull(driedcount,0)-@tiledcf as dried_count , @driedcf:=ifnull(driedcount,0) as x1 , @tiledcf:=ifnull(tiledcount,0) as x2 FROM ( SELECT @wk := 0 as init0 , @day1 := '2015-01-04' as init1 , @slab := (SELECT count(lot_id) FROM lot INNER JOIN block b USING (block_id) INNER JOIN community c on b.community_id = c.community_id AND c.contract_type_id IN (1,3) WHERE slab_date < @day1 AND IFNULL(dried_in_date,'9999-12-31')>=@day1) as init2 , @dried := (SELECT COUNT(lot.lot_id) FROM lot INNER JOIN block b USING (block_id) INNER JOIN community c on b.community_id = c.community_id AND c.contract_type_id IN (1,3) LEFT JOIN schedule s ON lot.lot_id=s.lot_id AND work_type_id=1 AND complete_date < @day1 WHERE dried_in_date < @day1 AND s.lot_id IS NULL) as init3 , @driedcf := 0 as init4 , @tiledcf := 0 as init5 ) init INNER JOIN ( SELECT COUNT(lot_id) as slabcount , DATE_FORMAT(slab_date, '%V') as week FROM lot INNER JOIN block b USING (block_id) INNER JOIN community c on b.community_id = c.community_id AND c.contract_type_id IN (1,3) WHERE DATE_FORMAT(slab_date, '%X') = DATE_FORMAT(@day1, '%X') GROUP BY week ) s LEFT JOIN ( SELECT COUNT(lot_id) as driedcount , DATE_FORMAT(dried_in_date, '%V') as week FROM lot INNER JOIN block b USING (block_id) INNER JOIN community c on b.community_id = c.community_id AND c.contract_type_id IN (1,3) WHERE DATE_FORMAT(dried_in_date, '%X') = DATE_FORMAT(@day1, '%X') AND dried_in_date is not null GROUP BY week ) d using (week) LEFT JOIN ( SELECT COUNT(s.lot_id) as tiledcount , DATE_FORMAT(complete_date, '%V') as week FROM lot INNER JOIN block b USING (block_id) INNER JOIN community c on b.community_id = c.community_id AND c.contract_type_id IN (1,3) INNER JOIN schedule s ON lot.lot_id=s.lot_id AND work_type_id=1 AND complete_date IS NOT NULL WHERE DATE_FORMAT(complete_date, '%X') = DATE_FORMAT(@day1, '%X') GROUP BY week ) t using (week) ;
×
×
  • 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.