I have a fair amount of experience using mysql and postgres, but on a
fairly simple level. My current task is to use "relations" to
simplify some queries. The table structure is as follows:
____table_1__________
id data1 data2 data3
____table_2__________
id email misc_data
the relationship is that table 2 may have many records with the same
email address; however, the id field is unique for that record.
Table 1 contains many records for each id.
What I need to do is get all the records in table 1 for a given email
address. If I wrote it in pretendSQL, I would write it as "SELECT *
FROM table_1 WHERE program IN (SELECT id FROM table_2 WHERE email =
'$given_email_address');
I think this might be a sub-select (again, I'm not experienced in
complex SQL queries). This is NOT what I want. Nor do I want to do a
join, if I can avoid it. A friend gave me a brief explanation, but
he's now unavailable. But basically, what he said was that you can
set up a relation between the the id and the email address, such that
you can just query table_1 as if the email address was actually a
field. In other words,
"SELECT * FROM table_1 WHERE email = '$given_email_address';
How would I do this using a relation, and can you give me the short
introduction to what is happening and how I can set this up in
postgres.
I suspect it's fairly simple and straightforward, and I'm pretty
perceptive. So give me the skinny. :-) |