Creating a view in Sql with connected tables

Jack Henry

I am trying to create a view off of two different tables. Basically I have 2 tables

(MatchStats- Holds all of the fixture information)

(Team - Holds all of the team information)

Here is the code for both tables

Team

    CREATE TABLE TEAM
(
TeamID int NOT NULL,
TeamName varchar(30) NOT NULL,
Teamlocale varchar(30) NOT NULL,

CONSTRAINT pkTeamID PRIMARY KEY (TeamID),

)

MatchStats

CREATE TABLE MATCHSTATS
(
MatchStatsID int NOT NULL,
Team1ID int NOT NULL,
Team1Goals int NOT NULL,
Team2ID int NOT NULL,
Team2Goals int NOT NULL,
RefereeID int NOT NULL,
PitchID int NOT NULL,
sessionTime varchar(20) NOT NULL,
sessionDate varchar(20) NOT NULL,

CONSTRAINT pkMatchStatsID PRIMARY KEY (MatchStatsID),
CONSTRAINT fkTeam1ID FOREIGN KEY (Team1ID) REFERENCES TEAM(TeamID),
CONSTRAINT fkTeam2ID FOREIGN KEY (Team2ID) REFERENCES TEAM(TeamID),
CONSTRAINT fkReferee FOREIGN KEY (RefereeID) REFERENCES REFEREE(RefereeID),
CONSTRAINT fkpitchID FOREIGN KEY (PitchID) REFERENCES Pitch(pitchID),
)

I am trying to create a view that will display the fixture information but use the team name instead of ID. I have been researching and looking at previous questions online and I just cant seem to understand how to do it

This is what I have managed to come up with

    CREATE VIEW FIXTUREHSITORY AS
SELECT m.Team1ID, m.Team1Goals, m.Team2ID, m.Team2Goals, T.TeamName
from MATCHSTATS as m 
JOIN TEAM as t ON t.TeamID on

Overall I am trying to create a report that looks like this

Team 1 Name | Goals | Team 2 Name | Goals

Sorry if I seem vague and thanks in advance

Nishant Gupta

A solution to your problem:

MSSQL

CREATE VIEW FIXTUREHISTORY AS
SELECT t1.TeamName Team1Name, 
       m.Team1Goals Team1Goals, 
       t2.TeamName Team2Name, 
       m.Team2Goals Team2Goals
from MATCHSTATS as m 
INNER JOIN TEAM as t1 ON t1.TeamID = m.Team1ID
INNER JOIN TEAM as t2 ON t2.TeamID = m.Team1ID

Demo Link:

http://sqlfiddle.com/#!18/34883/1

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

SQL : Creating View using MAX() + SUM() with 3 related tables

分類Dev

How to view that Advertiser is connected

分類Dev

Proc sql: Creating a view by copying data from one year to another

分類Dev

Creating a SQL view and removing numbers + and () från result

分類Dev

creating tables in mysql database

分類Dev

Creating a bubble view for messages

分類Dev

Hibernate Creating Unwanted Mapping Tables

分類Dev

Creating individual View Controllers for each view iOS?

分類Dev

Odoo. Creating empty view

分類Dev

Creating a open street maps view

分類Dev

Creating a iOS scrollable timeline view

分類Dev

Flask-SQLAlchemy creating schema before creating tables

分類Dev

pandas creating new table from two tables

分類Dev

Tables not showing anything when creating new one

分類Dev

Creating foreign key between 2 tables

分類Dev

What is the best way of creating tables in MySQL?

分類Dev

Creating a SQL totals table

分類Dev

SQL query with 3 tables

分類Dev

SQL Search All Tables

分類Dev

The view connected with IBOutlet property in UITableViewCell can not change frame

分類Dev

Programmatically creating constraints bound to view controller margins

分類Dev

Can have A column Like this when creating a view

分類Dev

Mysql creating a view based on existing table

分類Dev

Add Style after creating a View programmatically

分類Dev

Creating a callout with a custom view for ArcGIS Runtime for iOS

分類Dev

Where to view SQLite tables Django created?

分類Dev

Create multiple view tables from a single query

分類Dev

recursive view using lookup tables with ambiguous joins

分類Dev

Creating SQL RDS instance in CloudFormation

Related 関連記事

ホットタグ

アーカイブ