第一个是保存一般信息的地方表,第二个是用户注册的等候表(如等候名单)
+---------+--------------+------+-----+-------------------+----------------+
| 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的地方;并在主页上显示数据。像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,该数据将数据添加到等待表或检入抓取主页数据的模型。从我读过的内容来看,第二种选择会更好。或者我应该使用两个单独的查询?
答案 0 :(得分:1)
请阅读精选查询中的联接。看起来您需要在主表和临时表之间使用左外连接:http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php。
答案 1 :(得分:1)
我认为您的数据库设计是错误的:您应该使用特定于用户的数据(名称,图片,...)和users
创建单独的user_id
表。另一个包含“常规”信息的表(正如您所说):name,desc,map等。在此表中,仅使用用户特定信息user_id
。
如果您的数据库不是太大,您可以使用带有效select
的{{1}}代码,这样您就不需要验证了。
编辑如果您想知道user_id
表中没有的user_id
是什么,请使用类似的查询:
wait
这些SELECT user.userid
FROM user
LEFT JOIN wait ON user.userid=wait.userid
WHERE ISNULL(wait.place_id)
可以放入userid
- 列表。
答案 2 :(得分:0)
你可以使用像这样的查询:
select *
from wait_table
left join general_info_table on wait_table.user_id = general_info_table.user_id
where wait_table.user_id = 1;
这样,如果user_id在wait_table中,它会返回客户端上的信息...如果表中不存在,那么,应该返回null。
我会从查询中筛选出我真正需要的表字段。