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.

2
  • Post your table schema Commented May 16, 2015 at 10:21
  • @Uchiha see updates. Commented May 16, 2015 at 10:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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;

5 Comments

following error is occurring: <br \> #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
I you facing syntax error, becasue i can't test lack of db. you can use the link- stackoverflow.com/questions/15209414/mysql-update-join
Remove those quotes from column name
@hitesh Thanks for helping.<br \> can u tell me one thing more please <br \> in database connection $con=mysql_connect("localhost", "root",""); root location is where ? were should i store my database in xamp folder?
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.