简体   繁体   English

合并mysql中的列条目

[英]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. PeterM帮助我解决了这个问题,但是我不想创建一个新的问题,这就是为什么我要编辑这个问题。 I got another problem, sorry, im just new with sql. 我遇到另一个问题,对不起,我是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". 我想要的是:我创建了名为“ customers”且表名为“ id and customername”的表。 Then i created simple script with INSERT INTO "customers", yes, its script like admin page. 然后,我用INSERT INTO“ customers”创建了简单的脚本,是的,它的脚本类似于管理页面。 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. 这个想法是:我想从一个单独的页面添加新订单,例如:admin.php,因此我不必从文件中添加新订单。 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. 我的admin.php文件包含:INSERT INTOcustomers(客户名称)VALUES(customername)及其工作方式,我可以向数据库中添加新订单,当我填写html表单时,它不会显示我的新添加订单。

<?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: 我的HTML表单:

<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. 首先,你需要减去startdateenddate ,然后让所有的分组结果的总和,因此SUM的一部分。

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. time栏是您需要的。

I made a fiddle where you can see and test how it works, http://sqlfiddle.com/#!9/40a959/5 我做了一个小提琴,您可以在其中查看和测试它的工作原理, http://sqlfiddle.com/#!9 / 40a959 / 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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