需要与表进行比较并显示一行数据代替其他数据

时间:2013-08-01 15:53:21

标签: php mysql

好吧,让我们在表users_chars中看起来像这样

pos_zone
255
表二中的

zone_id | name
 255    | This_Area

我将如何比较它们并显示名称行而不是id

2 个答案:

答案 0 :(得分:1)

select t.name
from users_chars c
inner join table_two t on c.pos_zone = t.zone_id
where c.pos_zone = 255

答案 1 :(得分:1)

select t.name
from users_chars uc
inner join table_two t on uc.pos_zone = t.zone_id

这意味着什么:

select t.name告诉数据库要显示/检索

数据的列

from users_chars uc从users_chars表中获取数据并为其指定别名“uc”(如果UC中不存在该ID,则无法从table_two获取该名称)

users_chars表中的

inner join table_two t on uc.pos_zone = t.zone_id pos_zone列包含与table_two中的zone_id相同的数据,因此将这些数据链接在一起(通常是外键关系,但不一定是这样)。同时给table_two一个别名“t”