If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Converting a sentence into an SQL Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-11, 15:57
midnightkomp midnightkomp is offline
Registered User
 
Join Date: Sep 2011
Posts: 6
Converting a sentence into an SQL Query

I am trying to complete an assignment and I am not having much luck with it.

I have 12 questions I need to convert into queries and hope the some help with one of the will start me in the right direction.

I believe I am supposed to be using Joins to link info from two tables but I can't get it to work.

Here are the two tables-
Gig-
GigId
idvenue
idband
Gigdate-
NumAttendees

band-
idband
bandname

The question I need to convert is-

Which band did not have any gigs?

Any help would be greatly appreciated!
Reply With Quote
  #2 (permalink)  
Old 09-29-11, 18:19
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,606
We need to take this in steps. What SQL statement would produce a list of everything you know about bands? What SQL statement would produce a list of everything you know about gigs?

-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
  #3 (permalink)  
Old 09-29-11, 18:41
midnightkomp midnightkomp is offline
Registered User
 
Join Date: Sep 2011
Posts: 6
The two queries would be-

select * from band;

select * from gig;
Reply With Quote
  #4 (permalink)  
Old 09-30-11, 05:20
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
as you point out you need to do a join between the two tables and as part of that query identify which which bands have no (or a null) gigID
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 09-30-11, 14:06
midnightkomp midnightkomp is offline
Registered User
 
Join Date: Sep 2011
Posts: 6
I can't get the join to work, I have tried it a hundred different ways.
Reply With Quote
  #6 (permalink)  
Old 09-30-11, 14:08
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by midnightkomp View Post
I can't get the join to work, I have tried it a hundred different ways.
pls show one or two of them
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 09-30-11, 14:14
midnightkomp midnightkomp is offline
Registered User
 
Join Date: Sep 2011
Posts: 6
Here is the last one I tried.

Select band.idband, gig.gigdate from gig join band on gig.bandname where gigdate = null;

Thanks
Reply With Quote
  #8 (permalink)  
Old 09-30-11, 14:21
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
try rewriting your SQL so it matches the syntax used in MySQL
MySQL :: MySQL 5.0 Reference Manual :: 12.2.8.1 JOIN Syntax
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #9 (permalink)  
Old 09-30-11, 14:36
teachme teachme is offline
Registered User
 
Join Date: Sep 2011
Posts: 14
Quote:
Originally Posted by midnightkomp View Post
Here is the last one I tried.

Select band.idband, gig.gigdate from gig join band on gig.bandname where gigdate = null;

Try this:
Code:
Select band.idband, gig.gigdate 
from gig join band 
on gig.bandid = band.bandid 
where gigdate is null;
Reply With Quote
  #10 (permalink)  
Old 09-30-11, 15:05
midnightkomp midnightkomp is offline
Registered User
 
Join Date: Sep 2011
Posts: 6
I put that in and had to switch the bandid to idband in third line.


Select band.idband, gig.gigdate
from gig join band
on gig.idband = band.idband
where gigdate is null;

It comes up empty string, I believe the code is right but the actual databse does not have null for the bands that do not have gigs. It just does not list them.

I am going to need to adjust the gigdate with something else. How do I get it to list the bandname of the bands not listed?

Thanks for the help so far
Reply With Quote
  #11 (permalink)  
Old 09-30-11, 15:16
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
you are on the right path, but not quite there yet

you need a LEFT OUTER JOIN, for starters
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #12 (permalink)  
Old 09-30-11, 15:22
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
what relevance has the gigdate column in this query?
you are searching for bands that don't have any matching records in the gig table, so that column will always be null.

you need to refine the join to get the results you want.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #13 (permalink)  
Old 09-30-11, 15:26
midnightkomp midnightkomp is offline
Registered User
 
Join Date: Sep 2011
Posts: 6
The gigdate column only shows the actual dates of the bands that did have a gig. I would think the idband is the key. If a band's id is not listed then they do not have a gig.
Reply With Quote
  #14 (permalink)  
Old 09-30-11, 16:32
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,250
your original question is to find what bands didn't have any gigs
...so why do you think having gigdate in this query is relevant or useful?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On