【问题标题】:How to join two tables in MySQL using PHP and mysqli如何使用 PHP 和 mysqli 在 MySQL 中连接两个表
【发布时间】:2018-08-16 13:42:30
【问题描述】:

如何在 MySQL 中使用 PHPmysqli 连接两个表?

我有两张桌子:checkincheckout。我正在尝试将两个表与条件合并。

这是我的表结构:

签到

userid currentdate currenttime
60     08-03-2018   03:10
60     08-03-2018   05:50
60     08-03-2018   08:20
20     08-03-2018   01:04
60     09-03-2018   11:23
20     09-03-2018   10:24

结帐

userid currentdate currenttime
60     08-03-2018   04:05
60     08-03-2018   06:10
60     08-03-2018   09:25
20     08-03-2018   07:30
60     09-03-2018   12:30

我想要这样的结果:

Result

Userid Date        Time
60     08-03-2018   In:03:10  Out:04:05
                    In:05:50  Out:06:10
                    In:08:20  Out:09:25
20     08-03-2018   In:01:04  Out:07:30
60     09-03-2018   In:11:23  Out:12:30
20     09-03-2018   In:10:24    

这是 PHP 代码:

<?php
    include 'db.php';

    $sql = '
        SELECT 
            checkin.iduser as iduser,
            checkin.currentdate as currentdate,
            checkin.currenttime as currenttime,
            checkout.iduser as iduser2,
            checkout.currentdate as currentdate2,
            checkout.currenttime as currenttime2
        FROM checkin
        LEFT JOIN checkout ON checkin.iduser = checkout.iduser
    ';

    if($result = mysqli_query($con, $sql)) {
        if(mysqli_num_rows($result) > 0) {
            echo "  <table class=\"table table-bordered\">";
            echo "<tr>";
            echo "<th>ID</th>";
            echo "<th>Date</th>";
            echo "<th>Time</th>";
            echo "</tr>";

            while($row = mysqli_fetch_array($result)) {
                echo "<tr>";
                echo "<td>" . $row['iduser'] . "</td>";
                echo "<td>" . $row['currentdate'] . "</td>";
                echo "<td>In :"
                   . $row['currenttime'] 
                   . " <br> Out:" . $row['currenttime2']
                   . "</td>";
                echo "</tr>";
            }
            echo "</table>";
            mysqli_free_result($result);
        } else {
            echo "No records matching your query were found.";
        }
    } else {
        echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
    }
    mysqli_close($con);
?>

【问题讨论】:

  • 简短建议:将 SQL 部分和 PHP 分开。您可以使用 MySQL 命令行客户端(通常称为 mysql)运行 SQL 语句。首先这样做是为了找出正确的 SQL 语法,然后再考虑如何将其导入 PHP。
  • 请找到我的答案

标签: php mysql sql join


【解决方案1】:

假设在同一天没有相应的签入就没有结帐。 (例如:不能23:30签到次日00:30签出。)然后可以从checkin表中选择时间,在关联子查询中获取对应的签出时间。

select 
  i.userid,
  i.currentdate,
  i.currenttime as checkin,
  (
    select min(o.currenttime)
    from checkout o
    where o.userid = i.userid
      and o.currentdate = i.currentdate
      and o.currenttime > i.currenttime
  ) as checkout
from checkin i

结果会是这样的

| userid | currentdate | checkin | checkout |
|--------|-------------|---------|----------|
|     60 |  08-03-2018 |   03:10 |    04:05 |
|     60 |  08-03-2018 |   05:50 |    06:10 |
|     60 |  08-03-2018 |   08:20 |    09:25 |
|     20 |  08-03-2018 |   01:04 |    07:30 |
|     60 |  09-03-2018 |   11:23 |    12:30 |
|     20 |  09-03-2018 |   10:24 |   (null) |

演示:http://sqlfiddle.com/#!9/b43a46/5

您可以按日期、用户和签到时间对结果进行排序

order by i.currentdate, i.userid, i.currenttime

如果您的currentdate 格式为DD-MM-YYYY,您需要将其转换为可排序格式:

order by str_to_date(i.currentdate, '%d-%m-%Y'), i.userid, i.currenttime

您还可以使用另一个子查询按当天的第一次签到对用户进行排序:

order by i.currentdate, (
    SELECT MIN(currenttime)
    FROM checkin i2
    WHERE i2.userid = i.userid
      AND i2.currentdate = i.currentdate
  ),
  i.currenttime

现在由你来用 PHP 渲染 HTML 表格。

【讨论】:

    【解决方案2】:

    编辑你的$sql 替换它:

    $sql = '
        SELECT
            checkin.iduser as iduser,
            checkin.currentdate as currentdate,
            checkin.currenttime as currenttime,
            checkout.iduser as iduser2,
            checkout.currentdate as currentdate2,
            checkout.currenttime as currenttime2 
        FROM checkin
        LEFT JOIN checkout ON (checkin.iduser = checkout.iduser)
    ';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-24
      • 1970-01-01
      • 2012-04-27
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      相关资源
      最近更新 更多