简体   繁体   中英

Merge column entries in mysql

PeterM helped me with this question, but i dont want to create new one, thats why im editing this one. I got another problem, sorry, im just new with sql.

This is my code that output all the information I need from the database. What i want: I created table with name "customers" with columns "id and customername". Then i created simple script with INSERT INTO "customers", yes, its script like admin page. The idea is: I want to add new orders from a separate page, for example: admin.php so I don't have to do it from a file. How to easily do this? I have admin.php file with: INSERT INTO customers (customername) VALUES (customername) and its working, i can add new order to database, when i fill html form, it doesn't show my new added order.

<?php
$output = "SELECT *, SUM(enddate - startdate) AS time FROM employees GROUP 
BY id";
    $result = mysqli_query($conn, $output);
        WHILE ($row = mysqli_fetch_assoc($result)) {
            $EMPLOYEE = $row['employee'];
            $CUSTOMER = $row['customername'];
            $WORKDATE = $row['workdate'];
            $WORKTYPE = $row['worktype'];
            $DAYHOURS = $row['startdate'];
            $ENDDATE = $row['enddate'];
            $TOTAL = $row['time'];
            echo "
                <tr>
                <td>$EMPLOYEE</td>
                <td>$CUSTOMER</td>
                <td>$WORKDATE</td>
                <td>$WORKTYPE</td>
                <td>$DAYHOURS</td>
                <td>$ENDDATE</td>
                <td>$TOTAL</td>
                </tr>";
        }

?>

My html form:

<div class='row'>
<div class='col-25'>
<label for='customers'>Choose OrderName</label>
</div>
<div class='col-75'>
<select name='customername'>
<?php
$customers = "SELECT customername FROM customers";
$result = mysqli_query($conn, $customers);
WHILE ($row = mysqli_fetch_assoc($result)) {
$customer = $row['customername'];
echo "<option value=''>$customer</option>";
}
?>
</select>
</div>
</div>

You should change your query into something like this:

SELECT *, SUM(`enddate` - `startdate`) AS time FROM `employees` GROUP BY `employee`;

First you need to subtract the startdate from the enddate then get the sum of all the grouped results, hence the SUM part.

This should give you a result somewhat similar to this

Results :

| id | employee | startdate | enddate | time |
|----|----------|-----------|---------|------|
|  8 |    David |         8 |      22 |   17 |

The time column is the one you need.

I made a fiddle where you can see and test how it works, http://sqlfiddle.com/#!9/40a959/5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM