尝试查询两个表,我可以';我不知道怎么做


Trying to query two tables and I can't figure out how

第一个是保存一般信息的位置表,第二个是用户注册的等待表(如等待列表)

+---------+--------------+------+-----+-------------------+----------------+
| 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更好,还是在为主页获取数据的模型中进行检查更好。据我所知,第二种选择会更好。还是应该使用两个单独的查询?

我认为您的数据库设计是错误的:您应该用用户特定的数据(名称、图像…)和user_id创建单独的users表。还有一个包含"一般"信息的表(正如您所说):名称、描述、地图等。在这个表中,不使用仅user_id的用户特定信息。

如果数据库不是太大,则可以使用具有有效user_ids的select标记,这样就不需要验证。

EDIT如果您想知道wait表中没有的user_id是什么,请使用类似的查询:

SELECT user.userid
FROM user
LEFT JOIN wait ON user.userid=wait.userid
WHERE ISNULL(wait.place_id)

这些userid可以放入一个select-列表中。

请阅读select查询中的联接。看起来您需要在主表和临时表之间使用左外部联接:http://www.tizag.com/mysqlTutorial/mysqlleftjoin.php.

您可以使用这样的查询:

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。

不过,我会从查询中筛选出我真正需要的表字段。

相关文章: