![](/img/trans.png)
[英]I need to update a table joining two different tables. Is this query the appropriate one?
[英]SQL query joining two different tables. Find online status from user's last interaction
問題描述:根據Session
表中的最后一次交互查找治療師用戶的在線狀態。 不能在User
表上保留上次交互日期,但應該在Session
表上分開。 但我怎么能加入一個行,並與每一個用戶User
的表信息和會話數據Session
表,每個記錄用戶最近的互動?
圖 1 的描述:用戶表中的治療師列表,並根據他們在 Session 表中的最后一次交互顯示他們的在線狀態
圖2(User)說明:第一表是user表, email是主鍵。 它需要與第二列連接,即會話表的StartDate字段作為每條記錄的一行。 (粉紅色背景的StartDate
列不在User
表上。 User
表列加上粉紅色背景的StartDate
列是所需的結果)
問題最難的部分是email
是連接字段的基礎,並且在User
表中只有一個,但在Session
表中有很多,因為它不是主要的。
圖 3(會話)的描述:電子郵件字段隨着用戶在程序中的交互而重復。 它們是更改個人信息的按鍵日志,例如
我無法創建一個合格的查詢來解決我的問題,所以我在循環中為 table1 和第二個查詢創建了一個查詢,這不是完成這項工作的正確方式。 它導致了性能問題。 我需要幫助同時加入兩個不同的查詢。
第一個查詢根據User表中的主鍵email
過濾結果。 第二個查詢從循環內的會話表中獲取最新的會話日期。
$marker_list
是第一個查詢, $sql_online
用於嵌套的第二個查詢。
<?php
$latitude = filter_input(INPUT_GET, "latitude", FILTER_SANITIZE_SPECIAL_CHARS);
$longitude = filter_input(INPUT_GET, "longitude", FILTER_SANITIZE_SPECIAL_CHARS);
$start = filter_input(INPUT_GET, "start", FILTER_SANITIZE_SPECIAL_CHARS);
$email = filter_input(INPUT_GET, "email", FILTER_SANITIZE_EMAIL);
include_once "./connect-db.php";
if ($start == 'undefined') {
$start = 0;
} else {
$start = $start * 10;
}
$distance = 100;
//First query: lists therapists from User table
$marker_list = "SELECT `User`.`uid`, `User`.`latitude`, `User`.`longitude`, `User`.`name`, `User`.`phone_verified`, `User`.`email`, ( 3959 * acos(cos(radians(" . $latitude . ")) * cos(radians(latitude)) * cos(radians(longitude) - radians(" . $longitude . ")) + sin(radians(" . $latitude . ")) * sin(radians(latitude ))) ) AS `distance` FROM `massage`.`User` WHERE rol= 'terapist' AND list_on_maps= '1' AND email_valid='1' HAVING distance < $distance ORDER BY distance LIMIT $start, 7";
if ($results = mysqli_query($conn, $marker_list)) {
$sizeofquery = mysqli_num_rows($results);
$counter = 0;
$maps = array();
while ($row = mysqli_fetch_row($results)) {
$maps[$counter] = new stdClass();
$maps[$counter]->index = $row[0];
$maps[$counter]->latlng = new stdClass();
$maps[$counter]->latlng->latitude = floatval($row[1]);
$maps[$counter]->latlng->longitude = floatval($row[2]);
$maps[$counter]->title = $row[3];
$maps[$counter]->phone_verified = $row[4];
//Second query gets their user interaction from Session table
$sql_online= "SELECT `StartDate` FROM `massage`.`Session` WHERE `email`='$row[5]' ORDER BY `Session`.`StartDate` DESC LIMIT 1";
$result_nested= mysqli_query($conn, $sql_online);
$row_online= mysqli_fetch_array($result_nested);
$delta= (strtotime("now") - strtotime($row_online['StartDate']));
if ($delta < 300 ) {
$maps[$counter]->online = "y";
} else if ( $delta < 600){
$maps[$counter]->online = "t";
} else {
$maps[$counter]->online = "g";
}
$counter++;
}
$maps->counter = $row['counter'];
$mapsJSON = json_encode($maps);
}
//Third Query: Inserts last interaction as listing therapists as on many pages. It is not an important part of the question.
$sql_session = "INSERT INTO `massage`.`Session` (`StartDate`, `email`, `Interaction`) VALUES (current_timestamp(), '$email', 'dolistmaps');";
mysqli_query($conn, $sql_session);
header('Content-type:application/json;charset=utf-8');
echo $mapsJSON;
mysqli_free_result($results);
mysqli_close($conn);
所以請幫助我創建一個查詢,它完成在兩個表的公共字段email
上連接兩個不同的表。
連接兩個表的SQL
語法使用JOIN
關鍵字。 可以使用以下語法連接具有公共列的兩個表:
select StartDate
from Masage.Session masag
inner join User user on masag.email = user.email
這是用於inner join
。 如果您想要left outer join
或right outer join
,或者full outer join
甚至cross join
您應該用適當的關鍵字替換連接原因。 我希望我確實回答了你的問題。
您可以使用MAX
從Session
為派生表中的每封電子郵件查找最新的StartDate
並將其JOIN
到用戶表中以獲得您需要的結果:
SELECT `User`.`uid`,
`User`.`latitude`,
`User`.`longitude`,
`User`.`name`,
`User`.`phone_verified`,
`User`.`email`,
( 3959 * acos(cos(radians(" . $latitude . ")) * cos(radians(latitude)) * cos(radians(longitude) - radians(" . $longitude . ")) + sin(radians(" . $latitude . ")) * sin(radians(latitude ))) ) AS `distance`,
s.StartDate
FROM `massage`.`User`
JOIN (
SELECT email, MAX(StartDate) AS StartDate
FROM `massage.Session`
GROUP BY email
) s ON s.email = `User`.email
WHERE rol= 'terapist' AND list_on_maps= '1' AND email_valid='1'
HAVING distance < $distance
ORDER BY distance
LIMIT $start, 7
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.