Thanks and Help with Selection from two tables
Posted: Sun Sep 07, 2008 7:33 am
Deal All,
Thank you all for being of such great help to me over last few months in learning PHP/MySQL.
I do however get stuck on some codings and cannot find help, so I would like you to help me.
Scenario:
Two tables (Table1 and Table2)
Table1 Fields: -> ID, Name,Year
Table2 Fields: -> ID, Name
SQL query:
SELECT * FROM Table1,Table2
WHERE Table1.ID = Table2.ID
Result: This will generate all the records from the tables that has the same ID.
Problem:
If I want as above, all the records from both tables with the same ID along with records from table2 with a specific year (lets say 2008) in one report, how can I acheive this?
Clearification: I need the records from both tables that has the same Id (some will be in year 2008) and also all the remaining records in Table2 that does not have matching ID with Table1 but has Year 2008.
I know I can achive these separately as follows:
SQL query for same ID from both tables as already stated above:
SELECT * FROM Table1,Table2
WHERE Table1.ID = Table2.ID
SQL query for Year = 2008 in table2
SELECT * FROM Table2
WHERE Table2.Year=2008
How can I combine these two into one query for one report, please note that the two queries will have dupications as they are, because by matching IDs you will generate records with year 2008 also. I would like to avaoid duplication, these selected all with matching IDs in both tables and those records with Year = 2008 that were not selected with matching IDs but exist in Table1
If you need any other clarification I will try to explain further.
I appreciates all assistance.
Kindly help
Thanks
Dave
Thank you all for being of such great help to me over last few months in learning PHP/MySQL.
I do however get stuck on some codings and cannot find help, so I would like you to help me.
Scenario:
Two tables (Table1 and Table2)
Table1 Fields: -> ID, Name,Year
Table2 Fields: -> ID, Name
SQL query:
SELECT * FROM Table1,Table2
WHERE Table1.ID = Table2.ID
Result: This will generate all the records from the tables that has the same ID.
Problem:
If I want as above, all the records from both tables with the same ID along with records from table2 with a specific year (lets say 2008) in one report, how can I acheive this?
Clearification: I need the records from both tables that has the same Id (some will be in year 2008) and also all the remaining records in Table2 that does not have matching ID with Table1 but has Year 2008.
I know I can achive these separately as follows:
SQL query for same ID from both tables as already stated above:
SELECT * FROM Table1,Table2
WHERE Table1.ID = Table2.ID
SQL query for Year = 2008 in table2
SELECT * FROM Table2
WHERE Table2.Year=2008
How can I combine these two into one query for one report, please note that the two queries will have dupications as they are, because by matching IDs you will generate records with year 2008 also. I would like to avaoid duplication, these selected all with matching IDs in both tables and those records with Year = 2008 that were not selected with matching IDs but exist in Table1
If you need any other clarification I will try to explain further.
I appreciates all assistance.
Kindly help
Thanks
Dave