第一个是放置一般信息的位置表,第二个是用户注册的等待表(如等待列表)
+---------+--------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
| userid | int(3) | YES | | NULL | |
| address | varchar(300) | YES | | NULL | |
| desc | varchar(550) | YES | | NULL | |
| phone | int(15) | YES | | NULL | |
| image | varchar(50) | YES | | NULL | |
| website | varchar(100) | YES | | NULL | |
| cat | varchar(25) | YES | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | |
+---------+--------------+------+-----+-------------------+----------------+
+----------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| userid | int(11) | YES | | NULL | |
| place_id | int(11) | YES | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | |
+----------+-----------+------+-----+-------------------+----------------+
现在我正在做一个 SELECT * FROM place;并在主页上显示数据。像 tihs 这样的东西:
<? foreach($places as $place): ?>
<? echo $place->name; ?>; <? echo $place->userid; ?> etc ...
<a href="#">Click this to insert your userid and $place->id into wait table</a>
<? endforeach ?>
这是我迷路的地方。我想做类似的事情:
<? if($current_user_id == $userid_from_wait_that_matches_place_id): ?>
<p>You already registered for this!</p>
<? else: ?>
<a href="#">Click this to insert your userid and $place->id into wait table</a>
<? endif; ?>
不确定是在将数据添加到等待表的模型中检查用户 ID 还是在为主页抓取数据的模型中检查更好。根据我的阅读,第二种选择会更好。还是应该使用两个单独的查询?
最佳答案
我认为您的数据库设计有误:您应该创建单独的 users
表,其中包含用户特定的数据(名称、图像...)以及 user_id
。还有一个包含“一般”信息(如您所说)的表格:name、desc、map 等。并且在此表格中不使用特定于用户的信息,仅 user_id
。
如果您的数据库不是太大,您可以使用带有有效 user_id
的 select
标记,这样您就不需要验证。
编辑 如果您想知道wait
表中没有的user_id
是什么,请使用类似的查询:
SELECT user.userid
FROM user
LEFT JOIN wait ON user.userid=wait.userid
WHERE ISNULL(wait.place_id)
这些userid
可以放入select
列表中。
关于php - 试图查询两个表,但我不知道如何查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9096837/