简单的问题,但对我来说很难解决。=)
我有一个数据库 - USERS。在USERS中,我有两个表 - USERS_INFO和EVENTS。
USERS_INFO contain next fields:
user_id
user_name
...
和
EVENTS contains next:
event_id
user_id
obj_id (this element means, for examle, when user_1 will change information
event about user_2 in this table appears record like:
event_id=1
user_id=user_1
obj_id=user_2)
因此,正如您所见,来自表EVENTS的user_id,obj_1的信息来自字段user_id中的表USERS_INFO。 我连接了它。
问题是=如何创建正确的查询? 我需要看到这样的事情:
user_1 user_4 some_event_like_edit(表示user_1在user_4中更改了smth)
我可以创建查询,但它的工作错误。我做了 -
SELECT USERS_INFO.user_name, EVENTS.event FROM USERS_INFO,EVENTS WHERE USERS_INFO.user_id=EVENT.user_id
BUt我无法为另一个字段obj_id创建查询!! 结果为我提供EVENTS.user_id = EVENTS.obj_id
的字段我该怎么办?
答案 0 :(得分:0)
您只需要加入user
表两次,如下所示:
SELECT u_1.user_name as Who_Changed,u_2.user_name as Who_Got_Changed, e.event
FROM EVENTS e
INNER JOIN USERS_INFO u_1
ON (u_1.user_id=e.user_id)
INNER JOIN USERS_INFO u_2
ON (u_2.user_id=e.obj_id)