【发布时间】:2022-01-05 15:10:23
【问题描述】:
我的结果与表中出现的次数重复 device_transactions
my results currently
the result I want
- 当设备名称在表
device_transactions中没有对应记录或在表device_transactions中存在字段Ready >returned_date null - 当设备名称存在于
device_transactions表中具有字段return_date == null的对应记录时,状态返回Borrowing
Table devices:
| id | name |
|---|---|
| 1 | Projector |
| 2 | Laptop 001 |
| 3 | Laptop 002 |
| 4 | Humidifier |
| 5 | laptop 03 |
表device_transactions:
| id | device_id | start_transaction_plan | end_transaction_plan | returned_date |
|---|---|---|---|---|
| 1 | 1 | 2021-12-10 14:20:43 | 2021-12-12 07:00:00 | 2021-12-12 9:30:23 |
| 2 | 2 | 2021-12-11 10:10:20 | 2021-12-15 15:30:00 | 2021-12-16 7:30:45 |
| 3 | 3 | 2021-12-12 19:03:00 | 2021-12-21 08:00:00 | NULL |
| 4 | 4 | 2021-12-10 14:20:43 | 2021-12-12 07:00:00 | 2021-12-12 9:30:23 |
| 5 | 4 | 2021-12-11 10:10:20 | 2021-12-15 15:30:00 | NULL |
| 6 | 2 | 2021-12-12 19:03:00 | 2021-12-21 08:00:00 | 2021-12-16 7:30:45 |
| 7 | 2 | 2021-12-10 14:20:43 | 2021-12-12 07:00:00 | 2021-12-12 9:30:23 |
| 8 | 2 | 2021-12-11 10:10:20 | 2021-12-15 15:30:00 | 2021-12-16 7:30:45 |
| 9 | 2 | 2021-12-12 19:03:00 | 2021-12-21 08:00:00 | NULL |
| 10 | 1 | 2021-12-11 10:10:20 | 2021-12-15 15:30:00 | 2021-12-16 7:30:45 |
下面是我做的源码
device.php
<?php
function searchADVDevice()
{
require '../common/connectDB.php';
$keyword = '';
$status = '';
if(isset($_GET['keyword'])){
$keyword = $_GET['keyword'];
}
if(isset($_GET['status'])){
$status = $_GET['status'];
}
$sqlSearchString = '';
if ($status == '') {
$sqlSearchString = "select
IF((device_transactions.device_id IS NULL OR (device_transactions.returned_date IS NOT NULL AND device_transactions.device_id IS NOT NULL)),1,2) as status,
devices.id, devices.name, device_transactions.returned_date, device_transactions.device_id FROM devices
LEFT JOIN device_transactions ON devices.id = device_transactions.device_id
where name like '%$keyword%'";
}
else if ($status == 1) {
$sqlSearchString = "select 1 as status, devices.id, devices.name, device_transactions.returned_date,
device_transactions.device_id FROM devices
LEFT JOIN device_transactions ON devices.id = device_transactions.device_id where
(device_transactions.device_id IS NULL OR (device_transactions.returned_date IS NOT NULL AND device_transactions.device_id IS NOT NULL)) AND
devices.name like '%$keyword%' ";
}
else if ($status == 2) {
$sqlSearchString = "select 2 as status, devices.id, devices.name, device_transactions.returned_date,
device_transactions.device_id FROM devices
LEFT JOIN device_transactions ON devices.id = device_transactions.device_id where
device_transactions.device_id IS NOT NULL AND device_transactions.returned_date IS NULL AND
devices.name like '%$keyword%' ";
}
$sqlDeviceSearch = $conn->prepare($sqlSearchString);
$sqlDeviceSearch->execute();
$resultSearch = $sqlDeviceSearch->fetchAll() ;
return $resultSearch;
}
?>
device_search_advaned_controller.php
<?php
require '../model/device.php';
$resultSearch = searchADVDevice();
?>
SearchDevice.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TÌm kiếm thiết bị</title>
<link rel="stylesheet" href="../../web/css/device/searchAdvancedDevice.css">
</head>
<body>
<div class="content container">
<form action="" method="GET">
<div class="search">
<div class="search_keyword">
<label>Key word</label>
<input type="text" value="<?php if(isset($_GET['keyword'])) echo $_GET['keyword'] ?>" name="keyword">
</div>
<div class="search_status">
<label>Status</label>
<select id="status" name="status">
<option value=''>All</option>
<option value="1" <?php if(isset($_GET['status']) && $_GET['status'] == 1) echo 'selected' ?> >Ready</option>
<option value="2" <?php if(isset($_GET['status']) && $_GET['status'] == 2) echo 'selected' ?>>Borrowing</option>
</select>
</div>
<div>
<button type="submit" id="btn_search">Search</button>
</div>
</div>
</form>
<?php require '../controller/device_search_advaned_controller.php'; ?>
<div class="count_device">
<p>Number of devices found: <?php echo count($resultSearch) ?></p>
</div>
<table>
<tr>
<th id="th_no">No</th>
<th id="th_name">Device name</th>
<th id="th_status">Status</th>
<th id="th_action">Action</th>
</tr>
<?php
foreach ($resultSearch as $row) { ?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['name'] ?></td>
<td>
<?php
if (isset($row['status']) && $row['status'] == 2 ) {
echo "Borrowing";
} else {
echo "Ready";
}
?>
</td>
<td>
<?php
if (isset($row['status']) && $row['status'] == 1) {
$b='<button id="btn_borrow"><a href="device_borrow_view.php">Borrow</a></button>';
echo $b;
}
?>
</td>
</tr>
<?php
}
?>
</table>
</div>
</body>
</html>
【问题讨论】:
-
其中一种方式:select distinct xxxxx from [tablename] where [conditions....]
-
根据我对您的要求的理解,您只想在有人不使用时显示或让用户借用物品,即
returned_date不是NULL。像 Ken Lee 评论一样,只需使用distinct和条件returned_date not NULL。所有if都可以替换为一个distinct查询 -
@zimorok 你能在我的帖子中举一个具体的例子吗?因为我还不知道如何使用
distinct,所以我对如何使用感到很困惑:(( -
请注意,给定的查询对 SQL 注入开放。即使您使用准备好的语句,也不应该使用字符串连接来添加用户提供的数据