goran mm
goran mm

Reputation: 1

How to link two different tables and update the second one?

I have two tables, A and B.

Table A  .Birthday  .Name    .LastName
Table B  .Date      .Salary  .Rent

I need to write a query that increases Salary by 10% for everybody born on (Birthday=) 05/05/1977.

I don't know how to link these two tables and make it update table B.

Upvotes: 0

Views: 115

Answers (3)

mohsensajjadi
mohsensajjadi

Reputation: 335

As previously mentioned by fellow distinguished users, you NEED the PK in Table A and FK in Table B, otherwise it is impossible to join. But to do the actual updateing a better sql query would be:

Update b set b.Salary = b.Salary*1.1
From TableB b Inner Join TableA a On b.Id = a.Id
Where a.Birthday = '05/05/1977'

Upvotes: 1

Awais Qarni
Awais Qarni

Reputation: 18016

My dear first you have to link these two tables. Create a field of a_id in your A table and use that field as primary key in A table. Then also create a field a_id as a foreign key in you B table. After that you will be able to write the query

$q="select a_id from A where birthday='05/05/1977'";
$rs= mysql_query($q);
if($rs && mysql_num_rows($rs))
{
 whlile($rd=mysql_fetch_object($rs))
 {
  $q1="update B set salary=(salary*1.1) where aid=$rd->aid";
  $rs=mysql_query($q1);
 }
}

Upvotes: 0

Clodoaldo Neto
Clodoaldo Neto

Reputation: 125444

As they don't have a common column it is not possible to join those tables.

Upvotes: 1

Related Questions