Jump to content
kevin.upshaw

SQL Inner Join - Issue

Recommended Posts

Hi.Wonder if you can help, am quite new to SQL. In simple terms, I have a transaction table - and in that table are references to other related tables. In this simple query I am trying to obtain the first name and last name from the user ID for a transaction. SELECT transaction.transactionId, user.firstName, user.lastNameFROM [transaction]INNER JOIN [user]ON transaction.userId = user.userIdORDER BY user.userId However I get the response "incorrect syntax near the keyword transaction". I then try: SELECT 'transaction'.transactionId, user.firstName, user.lastNameFROM [transaction]INNER JOIN [user]ON 'transaction'.userId = user.userIdORDER BY user.userId I then receive "cannot call methods on varchar" - which I am assuming SQL thinks I am referring to text - rather than table names. I then tried: SELECT dbo.transaction.transactionId, user.firstName, user.lastNameFROM [transaction]INNER JOIN [user]ON dbo.transaction.userId = user.userIdORDER BY user.userId However no joy - I get the initial error message. Is someone able to point me in the right direction as to where I am going wrong? I have quite a few joins I need to do - but I thought if I could get this fixed first that would be a start.Cheers. Kev

Share this post


Link to post
Share on other sites

Your code doesn't seem to follow the tutorial. Please see: http://www.w3schools..._join_inner.asp Then check back.

Edited by niche

Share this post


Link to post
Share on other sites

Hi, I tried the following: SELECT transaction.transactionId, user.firstName, user.lastNameFROM transactionINNER JOIN userON transaction.userId=user.userIdORDER BY user.userId No joy. It wouldn't perhaps be because my table names are reserved names? I know "user" is. Thanks. Kevin

Share this post


Link to post
Share on other sites

what error messages are you getting now? Your code looks right depending on the environment you're using. Are you using php, myphpadmin, terminal...?

Share this post


Link to post
Share on other sites

I receive the error: "Incorrect syntax near the keyword 'transaction'". I am using WebMatrix - Using ASP.NET Web Pages - Razor Syntax (C#). However I am trying to do the query ion the WebMatrix environment before trying to code it into a web page/ Am running SQL Express on my laptop. Could perhaps try installing Visual Studio 2010 and running the query from there?

Share this post


Link to post
Share on other sites

Can you run it from a php script?

Share this post


Link to post
Share on other sites

Cracked it!It took someone else to go through it with me to realise what it is. I thought "transaction" and "user" were displayed in a colour they shouldn't be in on the WebMatrix software. So I created a bogus "transaction1" and "user1" table and put some fake data in them - then created the relationship between them. The query works absolutely fine now - so I'm guessing it was because I was using reserved words as my table names - will make sure I use proper names next time, would have saved so much time! Thank's very much for your help. Much appreciated. Kevin

Share this post


Link to post
Share on other sites

You're welcome. Feel free to check back any time you have a code question or want to learn something new. There are plenty of people that are far more knowledgeable than me available to help you.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...