SQL 查询:连接(或 select)来自 1 table 的 2 列和来自另一个 table 的 1 列,用于没有额外连接列的视图
SQL Query: Join (or select) 2 columns from 1 table with 1 column from another table for a view without extra join columns
这是我的第一个 Whosebug post,如果我没有正确格式化我的问题,我深表歉意。我确信这是一个简单的问题,我正在用头撞墙。我有一个 table,里面有一堆事件信息,大约有 10 列:
Table: event_info
date location_id lead_user_id colead_user_id attendees start end <and a few more...>
------------------------------------------------------------------------------------------------
2020-10-10 1 3 1 26 2100 2200 .
2020-10-11 3 2 4 18 0600 0700
2020-10-12 2 5 6 6 0800 0900
还有另一个 table 用户信息:
Table:用户
user_id user_name display_name email phone city
----------------------------------------------------------------------
1 Joe S goofball ...
2 John T schmoofball ...
3 Jack U aloofball ...
4 Jim V poofball ...
5 Joy W tootball ...
6 George A boring ...
我想创建一个只有信息子集的视图,而不是完整的 table 连接。事件 table lead_user_id
和 colead_user_id
列均引用 users
table.
中的 user_id
列
我想创建这样的视图:
date Location Lead Name CoLead Name attendees
---------------------------------------------------------------------
2020-10-10 1 Jack U Joe S 26
2020-10-11 3 John T Jim V 18
2020-10-12 2 Joy W George A 6
我已经尝试了以下和类似的几次迭代都无济于事...
SELECT
E.date, E.location,
U1.display_name AS Lead Name,
U2.display_name AS CoLead Name.
E.attendees
FROM
users U1, event_info E
INNER JOIN
event_info E ON U1.user_id = E.lead_user_id
INNER JOIN
users U2 ON U2.user_id = E.colead_user_id
我得到了可怕的
You have an error in your SQL Syntax
留言。我并不感到惊讶,因为我真的只在单个列或嵌套的 select 语句上使用过连接......这两列指向一个让我陷入循环。求助!
关于此事的正确查询
SELECT
E.date, E.location,
U1.display_name AS Lead Name,
(select display_name from users where user_id=E.colead_user_id) AS CoLead Name,
E.attendees
FROM
event_info E
INNER JOIN
users U1 ON U1.user_id = E.lead_user_id
这是我的第一个 Whosebug post,如果我没有正确格式化我的问题,我深表歉意。我确信这是一个简单的问题,我正在用头撞墙。我有一个 table,里面有一堆事件信息,大约有 10 列:
Table: event_info
date location_id lead_user_id colead_user_id attendees start end <and a few more...>
------------------------------------------------------------------------------------------------
2020-10-10 1 3 1 26 2100 2200 .
2020-10-11 3 2 4 18 0600 0700
2020-10-12 2 5 6 6 0800 0900
还有另一个 table 用户信息:
Table:用户
user_id user_name display_name email phone city
----------------------------------------------------------------------
1 Joe S goofball ...
2 John T schmoofball ...
3 Jack U aloofball ...
4 Jim V poofball ...
5 Joy W tootball ...
6 George A boring ...
我想创建一个只有信息子集的视图,而不是完整的 table 连接。事件 table lead_user_id
和 colead_user_id
列均引用 users
table.
user_id
列
我想创建这样的视图:
date Location Lead Name CoLead Name attendees
---------------------------------------------------------------------
2020-10-10 1 Jack U Joe S 26
2020-10-11 3 John T Jim V 18
2020-10-12 2 Joy W George A 6
我已经尝试了以下和类似的几次迭代都无济于事...
SELECT
E.date, E.location,
U1.display_name AS Lead Name,
U2.display_name AS CoLead Name.
E.attendees
FROM
users U1, event_info E
INNER JOIN
event_info E ON U1.user_id = E.lead_user_id
INNER JOIN
users U2 ON U2.user_id = E.colead_user_id
我得到了可怕的
You have an error in your SQL Syntax
留言。我并不感到惊讶,因为我真的只在单个列或嵌套的 select 语句上使用过连接......这两列指向一个让我陷入循环。求助!
关于此事的正确查询
SELECT
E.date, E.location,
U1.display_name AS Lead Name,
(select display_name from users where user_id=E.colead_user_id) AS CoLead Name,
E.attendees
FROM
event_info E
INNER JOIN
users U1 ON U1.user_id = E.lead_user_id