0

Table Schema:

Employee (empid, emp_name, age, DOB, designation, doj, basic_salary, salary_amt, medical_allowance, pension, GPFund, TA, House_allowance)
Monthly_salary (id, empid, year, month, total_salary)  

Query:

UPDATE `monthly_salary` 
     SET `total_salary`=`employee`.'salary_amt' -`employee`.'medical_allownces' - `employee`.'pension' 
     WHERE `employee`.'empid'=`monthly_salary`.'emp_id'

I'm trying to run this Query in phpmyadmin but its not running and giving errors. Employees empid is used as foreign key in monthly-salary table.

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43

2 Answers2

1

Remove the single qoutes '

Should looke like this

UPDATE `monthly_salary` salary
JOIN    `employee`      employee    ON employee.empid=salary.emp_id
SET     salary.total_salary=employee.salary_amt - employee.medical_allownces - employee.pension

I gave the tables alias names, makes it easier to read.

Mad Dog Tannen
  • 7,129
  • 5
  • 31
  • 55
0

join in update is not used in your query.

UPDATE monthly_salary a
    JOIN employee b  ON  b.empid = a.emp_id
SET a.total_salary=b.salary_amt - b.medical_allownces - b.pension;
Hitesh Mundra
  • 1,538
  • 1
  • 9
  • 13
  • following error is occurring:
    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''empid'=`monthly_salary`.'emp_id' SET `total_salary`=`employee`.'salary_amt' -`' at line 2
    – Khurram Shehzad May 16 '15 at 10:31
  • I you facing syntax error, becasue i can't test lack of db. you can use the link- http://stackoverflow.com/questions/15209414/mysql-update-join – Hitesh Mundra May 16 '15 at 10:32
  • Remove those quotes from column name – Narendrasingh Sisodia May 16 '15 at 10:33
  • @hitesh Thanks for helping.
    can u tell me one thing more please
    in database connection $con=mysql_connect("localhost", "root",""); root location is where ? were should i store my database in xamp folder?
    – Khurram Shehzad May 16 '15 at 10:35
  • @KhurramShehzad localhost is host where your mysql server is running. (localhost means your computer have ) root is user name, and "" contain password, which is currently blank. database is stored in xampp/mysql/data folder – Hitesh Mundra May 16 '15 at 10:41