Monday, 9 September 2013

Check if the ID of one table is used as a foreign key in another

Check if the ID of one table is used as a foreign key in another

I have a mysql database that has two tables:
GAME (ID, Name)
TICKET (ID, GameID, Section...)
Game.ID is a primary key in GAME, and GameID is used in the TICKET table
as a foreign key to know which game it is part of.
I have the sql statement:
SELECT * FROM GAME ORDER BY Date
It returns all game records from the GAME table. I want a sql statement
that will only return the game record if there is at least 1 record using
it as a foreign key in the TICKET table, thus only giving me the games
that have tickets.
I've tried joining the tables together and checking it that way, but my
sql just isn't there yet.
Thanks in advance for any help you can give me.

No comments:

Post a Comment