Jump to content

querys linking mysql tables


unistake

Recommended Posts

Hi all,

I am trying to link up two tables in a mysql database.

 

I am trying to create a script to do with refering. A user will be able to view their friends they have refered and see how much money they have in their account.

 

There are two tables I am using.

In table 'members' there are 2 columns 'email' and 'friend', both columns contain email addresses.

In table 'info' there are also 2 columns 'email' and 'price'.

 

In the members table, the field values in the friend column are the same as the values in the info table in the email column.

 

I am trying to SELECT {all the email addresses from table 'members' where friend=$user@hotemail.com} and then, once these email addresses have been selected, display all the friends emails along with their price in simple list. 

 

'members' table

email                                friend

friend1@hotletter.com        user@hotemail.com         

friend2@hotletter.com        user@hotemail.com

 

'info' table

email                                price

friend1@hotletter.com        32.99         

friend2@hotletter.com        31.49

 

The outcome list I am trying to display is:

user@hotemail.com friends prices

friend1@hotletter.com    £32.99

friend2@hotletter.com    £31.49

 

 

Thanks for the help

 

 

 

Link to comment
Share on other sites

Hi,

 

I think that you have got a little mixed up, either in your explanation or in the example of how the database data looks.

You said:

In the members table, the field values in the friend column are the same as the values in the info table in the email column.

However, from the example data, it looks like the coinciding column is the "email" column.

 

Also, it is a good idea to always have a unique numeric column in all tables and use this for referencing data between tables. (just a tip)

 

 

Anyway, assuming you are using mysql database you could join the tables something like this:

 

SELECT t1.friend, t2.email, t2.price FROM members AS t1 LEFT JOIN info AS t2 ON t2.email=t1.email WHERE t1.friend='user@hotemail.com'

 

That might not be exactly right but should be a good starting point.

 

Chris

 

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.