magician Posted February 2, 2012 Share Posted February 2, 2012 Hi I've been looking at the tutorial and spoken to a few people regarding a query which requires me to join two tables together and data within a specific data range. The environment I am running this in is SQL Server 2005. The following is the query I have tried to run: select *from individual (this is a table name)where update_timestamp <= 31/12/2008join delegate (this is a table name)as on member.member_ref=delegate.member_Refwhere start_date >= 31/12/2008 but when I run this, I get:Msg 156, Level 15, State 1, Line 4Incorrect syntax near the keyword 'join'. Someone advised me to try the following: select *from individualjoin delegateon member.member_ref=delegate.member_Refwhere start_date >= 31-dec-2008where update_timestamp <= 31-dec-2008 but when I do this, I get:Msg 156, Level 15, State 1, Line 6Incorrect syntax near the keyword 'where'. Can anyone advise me where I'm going wrong? If you require further info, please let me know. Thanks Link to comment Share on other sites More sharing options...
ShadowMage Posted February 2, 2012 Share Posted February 2, 2012 There are a few problems with how your query is written. First, the error message you're getting is because your dates need to be surrounded by single quotes. What are the data types of start_date and update_timestamp? Are they dates, strings, integers....? Second, you are trying to join the table 'individual' with the table 'delegate' on a field from a 'member' table. Third, your where clause is messed up. It should look something like this:.... WHERE <condition> AND <condition> Fourth, your conditions in the where clause are incorrect. Whenever you are using more than one table in a query, you must use the table name to reference the field:.... WHERE <table_name>.start_date Link to comment Share on other sites More sharing options...
justsomeguy Posted February 2, 2012 Share Posted February 2, 2012 You're also trying to join on fields in a table called member, but you're not using that table in the join. And in the first query, "as" and "on" don't go next to each other, you use "as" if you want to give the table an alias. Link to comment Share on other sites More sharing options...
ShadowMage Posted February 2, 2012 Share Posted February 2, 2012 You're also trying to join on fields in a table called member, but you're not using that table in the join.I believe I mentioned that. Second, you are trying to join the table 'individual' with the table 'delegate' on a field from a 'member' table. Link to comment Share on other sites More sharing options...
justsomeguy Posted February 2, 2012 Share Posted February 2, 2012 Well that's, like, your opinion, man. Link to comment Share on other sites More sharing options...
magician Posted February 3, 2012 Author Share Posted February 3, 2012 Thank you both for this. ShadowMage your advise definitley helped me. I took each section at a time and managed to put it right. Just need to rejig it to give me the fields I want now Thanks once again. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.