-1

So I have a table - Members - store the parents - Child - restore the child

I'm new to sql and my code is not working as expect, but you might be able to understand what I'm trying to accomplish here.

    set @variable1 = (select idMembers from members where firstname like '%James%')

    set @variable2 = (select FirstName, lastname, relationship from child where idMembers = @variable)

    print @variable2
shree.pat18
  • 21,449
  • 3
  • 43
  • 63
John Hang
  • 125
  • 7
  • 1
    MySQL or SQL Server? – shree.pat18 Jan 13 '16 at 08:34
  • 1
    your code wont work,you cant store multiple values in a single variable and if your result gives more than one row as output – TheGameiswar Jan 13 '16 at 08:36
  • it's mysql. So how do I accomplish something like this? From my understanding, @variable1 should only return 1. So I can use the 1 to return record from child that the foreign key column idMembers = 1. – John Hang Jan 13 '16 at 08:37

1 Answers1

0

I am recommending you to use JOIN to extract value from multiple related tables. However, as in your case, you are actually assign multiple column value to "variable2" which may be the problem.

BTW, you may want to read this topic to see the difference between SET and SELECT.

Community
  • 1
  • 1
Xinzoop
  • 111
  • 4
  • I saw that post but those codes doesn't work unfortunately. However I could use join, but both table have same column names. Will it affect? – John Hang Jan 13 '16 at 18:06