如何加入多重表? [英] How do I join multible tables?

查看:71
本文介绍了如何加入多重表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表(和示例值):

I have the following tables (and example values):

**user:**
user_id (1, 2, 3)
username (john33, reddiamond...)
password (pass1, pass2...)

**session:**
session_id (4,5, 6)
user_id (1, 2, 3)

**activity**
activity_id (1, 2)
name (running, walking...)

**user_activity**
user_activity_id (1, 2, 3, 4, 5)
session_id (4, 5)
activity_id (1, 2)

具有相同名称的所有列都相关.在表user_activity中,有几行描述了会话的活动,而该活动指的是用户.

All columns with the same name are related. In the table user_activity there are rows which describe what is the session's activity and the activity refers to users.

但是我想获得一张表,该表描述了用户当前正在做的事情:

However I would like to get the table which describes what the user is currently doing:

**result**
username(john33)
activity.name(walking)

获取结果表的SQL语句是什么?

What is the SQL statement that gets the result table?

(我正在使用MSSQL).

(I'm using MSSQL).

推荐答案

SELECT u.username, a.name
FROM user_activity ua
INNER JOIN session s
ON ua.session_id = s.session_id
INNER JOIN user u
ON s.user_id = u.user_id
INNER JOIN activity a
ON ua.activity_id = a.activity_id

这篇关于如何加入多重表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆