Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Statement to display data with comments from other table

Status
Not open for further replies.

cocopud

Technical User
Jan 8, 2002
139
US
I need some help with a select statement. Have been playing with this all afternoon and cannot get it to work. It seems like it should be relatively straightforward, but I usually only do basic SQL statement stuff, so not sure best way to approach this.

I have two tables, Table A has all the data and Table B is for comments and has 2 columns, the ID column and comment column. I need to create a SQL statement to display all the rows from Table A where the status = 'A' and also display the comments from Table B. Not all the rows will have comments and I have only been able to do this if the row has a comment. Anything else I try will list the rows with status ='A' once for every comment in Table B.

Any suggestions on how to do this.

Basically I want something like: SELECT * from A,B where A.status = 'A' or A.id = B.id

but when I try this I get the duplicates
 
Please..... learn about joins. It's obvious that you are a beginner, and we all started somewhere. I'm not trying to "put you down" for this, but I want you to realize that understanding joins is critically important.

Since table B might not always have a row for each row in the A table, you will want to use a left join for your query.

Code:
Select A.*, B.Comment
From   A
       Left Join B
         On A.Id = B.Id
Where  A.Status = 'A'

There are several different types of joins that you should learn about. Inner, Left, Right, Full, and Cross. Each type of join does something different, so it's important that you learn each one.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Thank you so much. I knew this was easy. I will look into joins in more detail. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top