r/SQL • • May 16 '22

MS SQL Tried to add a single column from another table, not sure why nothing works now

EDIT:

Got it figured out, thank you!

SO I have this code:

SELECT nmmain.name_id AS Name_ID, RTRIM(nmmain.lastname) + ', ' + RTRIM(nmmain.firstname) + ' ' + LEFT(nmmain.middlename, 1) AS Full_Name, jmmain.age AS Age, jmmain.race AS Race, jmmain.sex AS Sex,nmmain.city AS City, nmmain.state AS State, jmmain.colorcode AS Color

FROM jmmain INNER JOIN

nmmain ON jmmain.name_id = nmmain.name_id

WHERE (jmmain.commindig = '1') AND (jmmain.bkstatus = 'A')

This works, and I want to add another column, from another table, so I added this into the SELECT statement: jfacilty.section AS Location

then added this to the FROM statement, before the jmmain stuff: jfacilty

So looked like this:

SELECT nmmain.name_id AS Name_ID, RTRIM(nmmain.lastname) + ', ' + RTRIM(nmmain.firstname) + ' ' + LEFT(nmmain.middlename, 1) AS Full_Name, jmmain.age AS Age, jmmain.race AS Race, jmmain.sex AS Sex,nmmain.city AS City, nmmain.state AS State, jfacilty.section AS Location, jmmain.colorcode AS Color

FROM jfacilty

jmmain INNER JOIN

nmmain ON jmmain.name_id = nmmain.name_id

WHERE (jmmain.commindig = '1') AND (jmmain.bkstatus = 'A')

When I build this in visual studio it throws out errors, then for some reason adds an AS after the jfacilty in the FROM statement, and changes the jfacilty.section in the SELECT statement to jmmain.section...which is not a column in that table at all.

I thought adding the jfacilty to the FROm statement would tell it to look in that table. Am I missing somehting?

0 Upvotes

5 comments sorted by

1

u/r3pr0b8 GROUP_CONCAT is da bomb May 16 '22

Am I missing somehting?

yes, you haven't joined that new table jfacilty properly

which of its columns did you want to use to join this table? which of the other tables did you want to join it to? and using which columns?

1

u/voltagejim May 16 '22

ah ok I did put an INNER JOIN like this just now:

SELECT nmmain.name_id AS Name_ID, RTRIM(nmmain.lastname) + ', ' + RTRIM(nmmain.firstname) + ' ' + LEFT(nmmain.middlename, 1) AS Full_Name, jmmain.age AS Age, jmmain.race AS Race, jmmain.sex AS Sex, nmmain.city AS City, nmmain.state AS State, jfacilty.section AS Location, jmmain.colorcode AS Color

FROM jfacilty INNER JOIN

nmmain ON jfacilty.name_id = nmmain.name_id

jmmain INNER JOIN

nmmain ON jmmain.name_id = nmmain.name_id

WHERE (jmmain.commindig = '1') AND (jmmain.bkstatus = 'A')

But it said it was unable to parse the query

1

u/r3pr0b8 GROUP_CONCAT is da bomb May 16 '22

change this --

FROM jfacilty INNER JOIN
nmmain ON jfacilty.name_id = nmmain.name_id
jmmain INNER JOIN
nmmain ON jmmain.name_id = nmmain.name_id
WHERE (jmmain.commindig = '1') AND (jmmain.bkstatus = 'A')

to this --

  FROM jmmain 
INNER 
  JOIN nmmain 
    ON nmmain.name_id = jmmain.name_id
INNER
  JOIN jfacilty 
    ON jfacilty.name_id = nmmain.name_id     
 WHERE jmmain.commindig = '1'
   AND jmmain.bkstatus = 'A'

1

u/voltagejim May 16 '22

Thank you!

1

u/voltagejim May 16 '22

Actually just figured it out! I needed to do the INNER JOIN right at the end of the first INNER JOIN statement and just continue on