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 > Select Multi-Valued Attribute in separate table w/ Foreign Key

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-06-10, 11:49
jef3189 jef3189 is offline
Registered User
 
Join Date: Dec 2010
Posts: 2
Select Multi-Valued Attribute in separate table w/ Foreign Key

Hello, I have a database structure:

Table Users:
--------------------------------
email (PK) | firstname | lastname
--------------------------------
johndoe@blah.com | John | Doe

then a User can have many interests (multi-valued) so I created another table called interests.

Table Interests:
--------------------
email (FK) | interest
--------------------
johndoe@blah.com | Surfing
johndoe@blah.com | Fishing


Now I want to select all the interests of a user where the email=$email.

I simply did:
Code:
$query="SELECT interest from Interests WHERE email=$email"
However it is only retrieving the FIRST row where email=$email.
i.e. It is returning:
| johndoe@blah.com | Interests: Surfing.

How can I retrieve ALL values of interest where the email matches?
i.e I want to Display:
| johndoe@blah.com | Interests: Surfing, Fishing.
Reply With Quote
  #2 (permalink)  
Old 12-06-10, 12:01
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
presumably you are using PHP

text literals need to be encapsualted with a " or ' to delimit the value

try
PHP Code:
$query="SELECT interest from Interests WHERE email = '".$email."';" 
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old 12-06-10, 12:11
jef3189 jef3189 is offline
Registered User
 
Join Date: Dec 2010
Posts: 2
thanks healdem, I am already doing this even though I forgot to include them in my code snippet.

The query in my code is verbatim:

Code:
$query = "SELECT interest from Interests WHERE email = '$email'";
This works and retrieves only ONE tuple from the table for some reason. I need it to return multiple tuples. More specifically ALL the tuples where the email = $email.

Why does the query stop at the first match? Why does it not continue to return more tuples after it finds the first?
Reply With Quote
  #4 (permalink)  
Old 12-06-10, 13:37
healdem healdem is offline
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,262
so have you examined the sql being sent to the server?
have you checked to make certain there are multiple records?
have you run the same query that you are running in PHP in a query tool such as HeidiSQL or SQL query browser?
are you running your db server on *NIX or Windows.. Windows can have issues with capitalisation, whereas *NIX doesn't.. if you are runnign on windows check the column and table defintions

have you tried the variant of SQL I suggested?
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #5 (permalink)  
Old 12-06-10, 13:46
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
try this --
Code:
SELECT GROUP_CONCAT(interest) AS all_interests
  FROM Interests 
 WHERE email = '$email'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 12-07-10, 02:15
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Could you just show us the php you use to pull each record? I'm guessing you don't have a loop to pull each record.
__________________
Mike
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