專案- 資料庫名稱-test2
我的資料庫中有 2 個表
表 1 包含
Id Name Mobile Email
17 name1 1111 name1@test
18 name2 2222 name2@test
28 name3 3333 name3@test
表2包含
Sr. Id Paymobile Month Amount Date
1 28 3333 Jan 200 2021-01-06
2 28 3333 Feb 400 2021-02-06
3 28 3333 Apr 600 2021-04-08
4 17 1111 Mar 400 2021-03-05
6 18 2222 Aug 100 2021-08-27
7 17 1111 Jun 600 2021-06-21
table1 有單個和唯一的記錄,table2 包含付款歷史記錄,因此 id 和 mobile 有多次。
我想要什么 如果我搜索手機或電子郵件 ID,那么它還會在兩個表中給出結果,例如第一個表顯示 ID、名稱、移動、來自表 1 的電子郵件和第二個表顯示來自表 2 的月、金額、日期。一切都很好,但有一個問題在下面提到。
發出 當我搜索的手機號碼。3333 或 2222 在我的網頁中,然后它在兩個表中顯示兩個結果,就像我想要的一樣 table1 顯示 Id、Name、Mobile、Email 來自 table1 和第二個表顯示來自 table2 的 Month、Amount、Date。
但主要問題是它會多次顯示 Id、Name、Mobile、Email,盡管它在我的資料庫中有單條記錄。喜歡在這里輸入圖片描述
我的配置
1-form.php
<!DOCTYPE html>
<html>
<body>
<!-- (A) SEARCH FORM -->
<form method="post" action="1-form.php">
<h1>SEARCH FOR USERS</h1>
<input type="text" name="search" required/>
<input type="submit" value="Search"/>
</form>
<table>
<style>
table { font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #dddddd;
}
</style>
<tr>
<th>Id</th>
<th>Name</th>
<th>Mobile</th>
<th>Email</th>
</tr>
<?php
// (B2) DISPLAY RESULTS
?>
<?php
// (B) PROCESS SEARCH WHEN FORM SUBMITTED
if (isset($_POST["search"])) {
// (B1) SEARCH FOR USERS
require "2-search.php";
?>
<?php
// (B2) DISPLAY RESULTS
if(isset($_POST) && array_key_exists('search',$_POST)) {
if (count($results) > 0) { foreach ($results as $r) {
?>
<tr>
<td> <?php echo $r['Id'] ;?> </td>
<td> <?php echo $r['Name'] ;?> </td>
<td> <?php echo $r['Mobile'] ;?> </td>
<td> <?php echo $r['Email'] ;?> </td>
</tr>
<?php
}
}
} else {
echo "No results found"; }
}
?>
</table>
<p>
<table>
<tr>
<th>Month</th>
<th>Amount</th>
<th>Date</th>
</tr>
<?php
// (B2) DISPLAY RESULTS
if (count($results) > 0) {
foreach ($results as $r) {
?>
<tr>
<td> <?php echo $r['Month'] ;?> </td>
<td> <?php echo $r['Amount'] ;?> </td>
<td> <?php echo $r['Date'] ;?> </td>
</tr>
<?php
}
} else {
echo "No results found";
}
?>
</table>
</html>
和
2-search.php
<?php
// (A) DATABASE CONFIG - CHANGE TO YOUR OWN!
define("DB_HOST", "localhost");
define("DB_NAME", "test2");
define("DB_CHARSET", "utf8");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (B) CONNECT TO DATABASE
try {
$pdo = new PDO(
"mysql:host=".DB_HOST.";charset=".DB_CHARSET.";dbname=".DB_NAME,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]
);
} catch (Exception $ex) {
exit($ex->getMessage());
}
// (C) SEARCH
$stmt = $pdo->prepare("SELECT table1.*, table2.*
FROM table1
INNER JOIN table2 ON table1.Id = table2.Id
WHERE `Name` LIKE ? OR `Mobile` LIKE ?");
$stmt->execute(["%".$_POST["search"]."%", "%".$_POST["search"]."%"]);
$results = $stmt->fetchAll();
if (isset($_POST["ajax"])) {
echo json_encode($results);
}
uj5u.com熱心網友回復:
INNER JOIN 將顯示兩個表的所有匹配行。如果您點擊了在 table1 中出現 1 次但在 table2 中出現 3 次的 ID 28,您將總共獲得 3 行。
您可以GROUP BY Id向您的查詢添加一個或進行 2 個查詢 - 每個表一個 - 而不是帶有 INNER JOIN 的 1 個查詢。
uj5u.com熱心網友回復:
您可以使用關鍵字DISTINCT:
SELECT DISTINCT table1.*, table2.*
FROM table1
INNER JOIN table2 ON table1.Id = table2.Id
WHERE `Name` LIKE ? OR `Mobile` LIKE ?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402229.html
下一篇:在mysql表中查找父子回圈
