[英]SQL Query: Join (or select) 2 columns from 1 table with 1 column from another table for a view without extra join columns
This is my very first Stackoverflow post, so I apologize if I am not formatting my question correctly.这是我的第一篇 Stackoverflow 帖子,所以如果我没有正确格式化我的问题,我深表歉意。 I'm pounding my head against the wall with what I'm sure is a simple problem.
我的头撞在墙上,我确信这是一个简单的问题。 I have a table with a bunch of event information, about 10 columns as so:
我有一个包含一堆事件信息的表,大约有 10 列,如下所示:
Table: event_info表: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
And another table with user information:另一个包含用户信息的表:
Table: users表:用户
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 ...
I want to create a view that has only a subset of the information, not full table joins.我想创建一个只有信息子集的视图,而不是全表连接。 The event table
lead_user_id
and colead_user_id
columns both refer to the user_id
column in the users
table.事件表
lead_user_id
和colead_user_id
列都引用users
表中的user_id
列。
I want to create a view like this:我想创建一个这样的视图:
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
I have tried the following and several iterations like it to no avail...我已经尝试了以下和几次类似的迭代都无济于事......
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
And I get the dreaded我得到了可怕的
You have an error in your SQL Syntax
你的 SQL 语法有错误
message.信息。 I'm not surprised, as I've really only ever used joins on single columns or nested select statements... this two columns pointing to one is throwing me for a loop.
我并不感到惊讶,因为我真的只在单列或嵌套的 select 语句上使用过连接......指向一个的这两列让我陷入循环。 Help!
帮助!
correct query for this matter对此事的正确查询
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.