0

Hi I've got data in two tables my first table contains

  • first name
  • last name

my second table contain

  • userid
  • first name
  • last name

I'm trying to write a sql query to get the userid of a particular user but I'm getting empty set while executing the query. Could anyone please verify that the query I'm using is right? It seems ok to me

 select users.id 
 FROM TABLE1 AS r 
   LEFT JOIN TABLE2 AS users 
     ON (users.firstname = r.firstname 
     AND users.lastname=r.lastname)
4
  • Are you sure that firstname and lastname are exactly the same in both tables? Capitalization, trailing (and leading) spaces, all of these things are hidden gotchas. Commented Feb 10, 2012 at 12:43
  • 1
    Aren't you using the same table TABLE2. Commented Feb 10, 2012 at 12:46
  • 1
    Is the second table containing two columns or three? The definition implies two while the query implies three. Commented Feb 10, 2012 at 15:03
  • You should never join tables on a free-text field - it will lead to endless problems - especially when someone decides you need to support chinese and now your collations all need to change. Use Ids. Commented Feb 10, 2012 at 16:13

3 Answers 3

2

You use twice the same table (TABLE2), but in the description you state that you have two tables.

Sign up to request clarification or add additional context in comments.

2 Comments

sorry I'm mistaken, actually I meant select users.id FROM TABLE1 AS r LEFT JOIN TABLE2 AS users ON (users.firstname = r.firstname AND users.lastname=r.lastname)
as proposed by Brian Hoover, you may try to trim the fields you are using in your join condition.
1

I am not sure but i think you want this:

 select users.id 
      FROM TABLE1 AS r 
    INNER JOIN TABLE2 AS users 
        ON (users.firstname = r.firstname AND users.lastname=r.lastname)

Comments

0
select users.id 
      FROM TABLE1 AS r 
    INNER JOIN TABLE2 AS users 
        ON (lower(ltrim(rtrim(users.firstname))) = lower(ltrim(rtrim(r.firstname))) AND lower(ltrim(rtrim(users.lastname)))=lower(ltrim(rtrim(r.lastname))))

1 Comment

do you have single column for firstname and lastname in second table?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.