Jump to content

Select where one field occurs in another field in another table.


bskauge

Recommended Posts

Hello,

I thought this should be easy, but I just can't figure it out.

I have two tables, and I want to list every record from table A where 'name' is mentioned in a list of names in table B.

I've tried something like
SELECT * FROM table_a WHERE name IN ('$table_b[listofnames]')
and
SELECT * FROM table_a WHERE name LIKE '%$table_b[listofnames]%'

This works when $table_b[listofnames] only contains one name, but there is a comma separated list of names in that field. I see that $table_b[listofnames] returns the whole list, so the wildcards won't work since they are put at the beginning and at the end of the list. How do I do this? And what should I eventually do about double fornames (like "John Henry")?

I really hope someone can help me out?



Kindest regards,
Bjorn Skauge
Norway
Link to comment
Share on other sites

[quote]SELECT * FROM table_a a, table_b b WHERE a.name = b.name AND b.field1 = "x"[/quote]

a.name contains just one name, b.name is a long list of names. a.name = b.name will only match if b.name contains just one name. So I want to get all the records from table a where that one name from a.name is mentioned in the b.name list.
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.