use*_*164 -5 php mysql sql join
如何使用PHP和mysqli在MySQL中连接两个表?
我有两张桌子:checkin和checkout.我试图将两个表合并为一个条件.
这是我的表结构:
报到
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
Run Code Online (Sandbox Code Playgroud)
查看
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
Run Code Online (Sandbox Code Playgroud)
我希望得到这样的结果:
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
Run Code Online (Sandbox Code Playgroud)
这是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);
?>
Run Code Online (Sandbox Code Playgroud)
假设在同一天没有相应的签到没有结账.(例如:在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
Run Code Online (Sandbox Code Playgroud)
结果就是这样
| 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) |
Run Code Online (Sandbox Code Playgroud)
演示:http://sqlfiddle.com/#!9/b43a46/5
您可以按日期,用户和签入时间对结果进行排序
order by i.currentdate, i.userid, i.currenttime
Run Code Online (Sandbox Code Playgroud)
如果您currentdate的格式是DD-MM-YYYY您需要将其转换为可排序的格式:
order by str_to_date(i.currentdate, '%d-%m-%Y'), i.userid, i.currenttime
Run Code Online (Sandbox Code Playgroud)
您还可以使用另一个子查询在当天首次签入时对用户进行排序:
order by i.currentdate, (
SELECT MIN(currenttime)
FROM checkin i2
WHERE i2.userid = i.userid
AND i2.currentdate = i.currentdate
),
i.currenttime
Run Code Online (Sandbox Code Playgroud)
现在,您可以在PHP中呈现HTML表格.