如何将表连接在一起-SQL [英] How to join tables together - SQL

查看:64
本文介绍了如何将表连接在一起-SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个SQL问题

我需要使用内部联接语法将两个表联接在一起.一个命名为Entry,另一个命名为奖品.我需要列出event_id, horse_id, place, money.

I have two tables I need to join together, using the inner join syntax. One named Entry, and the other named prize. I need to list the event_id, horse_id, place, money.

ENTRY (TABLE NAME)
COLUMNS IN TABLE ENTRY:
Event_id,
Horse_id
Place

PRIZE (TABLE NAME)
COLUMNS IN TABLE PRIZE:
Event_id,
Place,
Money

据我所知,我只是无法理解.

This is as far as I have got, I just can't get my head around it.

SELECT event_id, horse_id, place
FROM ENTRY INNER JOIN PRIZE
ON ENTRY.money = PRIZE.money

谢谢

推荐答案

SELECT e.Event_id, e.Horse_id, e.place, p.money

FROM ENTRY e join Prize p

ON e.Event_id = p.Event_id

where e.place = p.place;

e和p用作表的别名,以避免由于表名太长而导致无法读取的sql.

The e and p are used as aliases for the tables to avoid unreadable sql because of long table names.

使用e.或第您将为该表选择字段,因为两个表都可能具有相同名称的字段,因此在执行语句时会出现问题

using the e. or p. you will select the field for that table because it is possible that both tables have a field with the same name so there will be issues when executing the statement

我添加了e.place = p.place,因为如果您不这样做,您将获得与每个奖项匹配的每个赛事的每个地方的结果

I added the e.place = p.place because if you don't you would be getting the results for every place for each event matched with every prize

例如,您会得到 赛事1马1地方1奖1 事件1马1地方1奖2 赛事1马1地方1奖3 赛事1马1地方1奖4 等等...直到获得所有奖项为止,并且假设奖品的活动等于参赛作品的活动,则每次参赛的奖项都是相同的

for example you would get Event 1 horse 1 place 1 prize 1 event 1 horse 1 place 1 prize 2 Event 1 horse 1 place 1 prize 3 event 1 horse 1 place 1 prize 4 etc... until you get every prize and this would be the same for every entry, assuming the event for the prize equals the event for the entry

这篇关于如何将表连接在一起-SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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