对于访问创建它的脚本的每个用户来说,mysql临时表是唯一的...?

发布于 2024-08-24 22:35:28 字数 1775 浏览 2 评论 0原文

当用户在特定日期之间搜索免费酒店时,在寻找一种临时保存搜索结果的方法时,我遇到了临时表。

但某些问题即使在 mysql 手册中也没有得到解答...比如...

  1. 临时表对于执行脚本的每个用户来说是唯一的吗...?或者当两个不同的用户同时运行脚本时它会被覆盖...?

  2. 表什么时候会被销毁..?当用户关闭浏览器窗口或只是离开脚本运行的页面时...?

感谢您的澄清...

这就是我的做法...

$table = "CREATE OR REPLACE TEMPORARY TABLE $free_room(
              room_id INT(5),
              room_name VARCHAR(150),
              max_persons INT(1),
              rooms_free INT(1),
              room_price INT(6),
              hotel_id INT(4),
              hotel_name VARCHAR(100),
              hotel_stars INT(1),
              hotel_type INT(1)) ENGINE=MEMORY";

    $query_getFreeRooms = "INSERT INTO  $free_room
                           SELECT $rooms.room_id,
                                  $rooms.room_name,
                                  $rooms.max_persons,
                                  $rooms.total_rooms - $rooms.booked_rooms AS rooms_free,
                                  $rooms.room_price,
                                  $hotels.hotel_id,
                                  $hotels.hotel_name,
                                  $hotels.hotel_stars,
                                  $hotels.hotel_type
                           FROM   $hotels,$rooms
                           WHERE  $rooms.room_id NOT IN (SELECT room_id
                                                         FROM   $reservations
                                                         WHERE  $dateCheck)
                           AND    $hotels.hotel_city = '$city_search1'
                           AND    $hotels.hotel_id = $rooms.hotel_id
                           AND    $hotels.hotel_deleted = '0'
                           AND    $rooms.room_deleted = '0'";

While looking for a way to temporarily save the search results when a user searches for a hotel free between particular dates i came across temporary tables.

But certain questions are not answered even in mysql manual.... like...

  1. Will the temporary table be unique for each user that executes the script...? Or will it be overwritten when two different users run the script at the same time...?

  2. When will the table be destroyed..? When the user closes the browser window or just navigates away from the page in which the script runs...?

Thanks for your clarifications...

This is how i do it....

$table = "CREATE OR REPLACE TEMPORARY TABLE $free_room(
              room_id INT(5),
              room_name VARCHAR(150),
              max_persons INT(1),
              rooms_free INT(1),
              room_price INT(6),
              hotel_id INT(4),
              hotel_name VARCHAR(100),
              hotel_stars INT(1),
              hotel_type INT(1)) ENGINE=MEMORY";

    $query_getFreeRooms = "INSERT INTO  $free_room
                           SELECT $rooms.room_id,
                                  $rooms.room_name,
                                  $rooms.max_persons,
                                  $rooms.total_rooms - $rooms.booked_rooms AS rooms_free,
                                  $rooms.room_price,
                                  $hotels.hotel_id,
                                  $hotels.hotel_name,
                                  $hotels.hotel_stars,
                                  $hotels.hotel_type
                           FROM   $hotels,$rooms
                           WHERE  $rooms.room_id NOT IN (SELECT room_id
                                                         FROM   $reservations
                                                         WHERE  $dateCheck)
                           AND    $hotels.hotel_city = '$city_search1'
                           AND    $hotels.hotel_id = $rooms.hotel_id
                           AND    $hotels.hotel_deleted = '0'
                           AND    $rooms.room_deleted = '0'";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-08-31 22:35:28

引用 MySQL CREATE TABLE 的手册页

您可以在以下情况下使用TEMPORARY关键字:
创建一个表。
TEMPORARY 表是
仅对当前可见
连接,并且已断开
连接时自动
关闭。
这意味着两个不同的
连接可以使用相同的临时
表名不与
彼此
或与现有的
同名的非临时表。


考虑到两个用户将有两个不同的连接,该表对于每个用户来说都是唯一的。

当创建临时表的连接关闭时,临时表将被删除 - 至少在 PHP 中,这意味着生成页面的脚本结束时(因此,通常,甚至在用户实际读取页面之前)< /em>

Quoting the MySQL manual page of CREATE TABLE :

You can use the TEMPORARY keyword when
creating a table.
A TEMPORARY table is
visible only to the current
connection, and is dropped
automatically when the connection is
closed
.
This means that two different
connections can use the same temporary
table name without conflicting with
each other
or with an existing
non-TEMPORARY table of the same name.

Considering that two users will have two distinct connections, the table will be unique for each user.

The temporary table will be dropped when the connection that created it is closed -- in PHP, at least, it means when the script that generates the page ends (So, generally, even before the users actually reads the page)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文