Help with SQL command

I have two tables:

table_1
table_2

table_1 has the following columns
b_id            - Primary Key
company
contact

table_2 has the following columns
sales_id     -Primary Key
company
contact
b_id           -currently null

I would like to create a sql query that will match the company from table_1 with company from table_2 and grab b_id from table_1 and put it in table_2 for b_id

Is this possible

LVL 1
digaratiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pzozulkaCommented:
Update table2
Set table2.b_id = table1.b_id
Where Table1.company = table2.company
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sm394Commented:
or

UPDATE t2
SET t2. b_id= t1.b_id
FROM table_2  t2  INNER JOIN table_1 t1
ON t2.company = t1.company
0
SharathData EngineerCommented:
UPDATE table_2
SET b_id= t1.b_id
FROM table_2  t2  INNER JOIN table_1 t1
ON t2.company = t1.company
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

SujithData ArchitectCommented:
Use this update stmt
update table_2 a
set b_Id = (select b.b_id
            from table_1 b
	    where b.company = a.company)
where exists (select 1
            from table_1 b
	    where b.company = a.company);

Open in new window

0
Mariner73Commented:
You can use upper for company, because they could be entered diferently

update table_2 a

set
 b_Id 
=
 
(
select
 b
.
b_id
            
from
 table_1 b
            
where
 
upper(b
.
company) 
=
 Upper(a
.
company
)
	    )


where
 exists 
(
select
 
1

            
from
 table_1 b
            
where 

upper(b
.
company)
 
=
 upper(a
.
company)

            );

Open in new window

0
SujithData ArchitectCommented:
>> You can use upper for company

But this could be a performance penalty. A function on the join columns prevent the possible usage of indexes on those columns.
Generally a well designed application populate data in a single case.
0
SujithData ArchitectCommented:
digarati,
can you explain how this "accepted" solution works for you?
It is not even complete in syntax
0
SharathData EngineerCommented:
I agree with Sujith's comments. Your accepted solution wont work for you.
0
Mariner73Commented:
To sujith80, generally I agree,
upper() is not best for performance, But it is obvious there is some inconsistency in data,
once he need to populate key column, so, application provided this data could be different.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.