Jump to content

Help building a query


Darkmatter5

Recommended Posts

Here are my tables

 

game_collections

gs_idmember_id

10122

2022

222

1622

 

game_systems

gs_idgame_idsystem_id

256

16816

2016

10146

 

games

game_idtitle

1Fable 2

4Fallout 3

5Blue Dragon

8The Legend of Zelda: Twilight Princess

 

systems

system_idname

6Xbox 360

16Wii

 

Now I'm wanting to get a list of games on a specific system that a specific member has in their collection.  So I'm trying to build a query to do this across all 4 tables.  PLEASE help!  Here's what I have so far.

 

SELECT g.title, s.name AS sysname
FROM game_collections gc
JOIN game_systems USING(gs_id) 
WHERE gc.member_id=22
AND s.system_id=6
ORDER BY g.title ASC

Link to comment
Share on other sites

SELECT g.title, s.name AS sysname
FROM games g, game_collections gc,
game_systems gs, systems s 
WHERE 
g.game_id = gs.game_id AND
gs.system_id = s.system_id AND
gc.gs_id = gs.gs_id AND
gc.member_id=22 AND 
s.system_id=6
ORDER BY g.title ASC

 

See if that works.

Link to comment
Share on other sites

Worked great!

 

Quick question though.  What are good practices for when and when not to use joining in a query versus just where x=x?

 

I never really understood the JOINS. I guess I should read up on it. Found this tid bit which may help you:

 

Different SQL JOINs

 

Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.

 

    * JOIN: Return rows when there is at least one match in both tables

    * LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table

    * RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table

    * FULL JOIN: Return rows when there is a match in one of the tables

 

From : http://www.w3schools.com/Sql/sql_join.asp

 

Also found this in Wiki http://en.wikipedia.org/wiki/Join_(SQL)

 

I am going to do some reading now :)

 

EDIT:

The join in the code I gave you is considered an "INNER JOIN" as an fyi :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.