2

I wanna write a query like this :

UPDATE `test_credit` 
SET `test_credit`.`credit`=(`test_credit`.`credit`-((`test_credit`.`credit`/100)*5)) 
WHERE `test_credit`.`name` = `users`.`uname`

in fact i want to get a query on users.uname = test_credit.name but mysql say it has error and realize users.uname as column

what is correct query ?

1 Answer 1

4

You need to explicitly join it with table users. As far from my understanding based on your query, you want to calculate the credit if the names exists on both tables.

Give this a try,

UPDATE  test_credit a
        INNER JOIN users b
            ON a.name = b.uname
SET     a.credit = (a.credit - ((a.credit/100) * 5.0)) 
-- WHERE  b.parent= "example"
Sign up to request clarification or add additional context in comments.

3 Comments

if you have further questions, just drop a comment :)
tanx my friend it works and if i say a condition i should write like this? UPDATE test_credit a INNER JOIN users b ON a.name = b.uname AND b.parent= "example" SET a.credit = (a.credit - ((a.credit/100) * 5.0))
since it is using INNER JOIN, you can add it on WHERE clause, see my update.

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.