Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combining records from a table with another based on another table

Status
Not open for further replies.
Jul 28, 2011
167
NG
Ok, I understand the title may not speak exactly what I want to achieve and this may also not be the right forum for it.
If my guess above is right, please help me redirect as appropriate.
Well, here's an issue I've been trying to work around on MySql (No problem if you are better with MSSQL. I understand the both).

I have a table called members (I use it to store registered members) like so:
Code:
member_id | createDate | first_name | last_name | street | state | country | phone | email   |
   1      | 2012-06-09 |   James    |  Ini      | xxx    |  xxxx |  U.S.A  | xxx   | a@y.com |
   2      | 2012-06-10 |  Brown     | Tony      | xxx    |  xxxx |  U.S.A  | xxx   | b@c.com |
Another table called comtacts like so
Code:
contact_id | contact_name  | contact_addr | contacts_country | contact_phone | contact_email
    1      |  Pats Ltd.    |  N.J         |  U.S.A           |  xxxxxx       | k@g.com
    2      | West minister |  N.Y.        |  U.S.A           |  xxxxxx       | y@z.com
    3      |  infinitizon  |  Calif       |  U.S.A           |  xxxxxx       | j@m.com
Finally, there is a table that links the two tables called buyers_bought like so
Code:
id   |   contacts_id   |   member_id   |   buyers_bought_date  |
1    |       1         |        1      |   2012-06-09          |
2    |       2         |        1      |   2012-06-10          |
3    |       2         |        2      |   2012-06-11          |

Now, when I run this query against the database
Code:
SELECT contacts.contact_id, buyers_bought.contacts_id, contacts.contact_name,
   (SELECT email FROM members WHERE member_id = buyers_bought.member_id) AS mem_email
   , contacts.contact_addr, contacts.contacts_country, contacts.contact_phone 	
   , contacts.contact_email
FROM contacts
LEFT JOIN buyers_bought
ON contacts.contact_id = buyers_bought.contacts_id
I get this result duplicate result for contacts that has been bought by more than one member.
So suppose I want to get all contacts when brown logs in, I get something like
Code:
contact_id | contacts_id | contact_name  |  mem_email  | contact_addr | contacts_country | contact_phone | contact_email
    1      |    1        | Pats Ltd.     | a@y.com     |  N.J         |  U.S.A.          |     xxxx     |  k@g.com
    2      |    2        | West minister | a@y.com     |  N.Y.        |  U.S.A.          |     xxxx     |  y@z.com
    2      |    2        | West minister | b@c.com     |  N.Y.        |  U.S.A.          |     xxxx     |  y@z.com
    3      |    NULL     | infinitizon   |  NULL       |  Calif       |  U.S.A           |  xxxxxx     |  j@m.com
The challenge is if b@c.com is the currently logged on user, it should give a result similar to the following
Code:
contact_id | contacts_id | contact_name  |  mem_email  | contact_addr | contacts_country | contact_phone | contact_email
    1      |    1        | Pats Ltd.     |  NULL       |  N.J         |  U.S.A.          |     xxxx     |  k@g.com
    2      |    2        | West minister | b@c.com     |  N.Y.        |  U.S.A.          |     xxxx     |  y@z.com
    3      |    NULL     | infinitizon   |  NULL       |  Calif       |  U.S.A           |  xxxxxx     |  j@m.com

Can anyone help me work this out
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top