Dennis Berg
Dennis Berg

Reputation: 201

mysql select data by relation of parameters

some help here please with (advanced) mysql. Example illustrates the issue much better. So..

I have a predefined value pair table PARTNERS with partner1 and partner2 columns. And then there is another table PROJECTS which has partner1 and partner2 columns.

So, I want to select all PROJECTS where columns partner1 and partner2 are within the actual partners table described above.

Obviously, I cannot simply indicate "where partner1=... and/or partner2=..." because there would be no check of whether those two are partners (as defined in the PARTNERS table). (Just in case: there are projects with partner1 and partner2 not in the partner relationships.)

I don't think that kind of query is possible at all since it has the condition stored in the result. But would be happy if one can prove otherwise. Thanks

Upvotes: 0

Views: 77

Answers (1)

octern
octern

Reputation: 4868

It sounds like you need a query that will give you rows from PROJECTS where partner 1 and partner 2 are both the same as they are in any row in PARTNERS. So do an inner join:

SELECT * FROM projects
INNER JOIN partners
    ON projects.partner1 = partners.partner1
    AND projects.partner2 = partners.partner2

If you're up for restructuring your tables, it might make more sense to assign a unique ID to every row in PARTNERS, and use that ID in the PROJECTS table. That way instead of storing two columns of redundant data, you just have one column indicating which pair of students is on the project.

Upvotes: 2

Related Questions