0

I already create a function called calculate_marks(user_id) and it run properly, but when i put in the insert statement, it will keep running in mysql and no respond, anyone know what is the problem?

INSERT INTO `marks`
(`marks_user_id`, `marks_addtime`, `marks_amount`
) 
SELECT `user_id`, UNIX_TIMESTAMP(),
    (SELECT calculate_marks(`user_id`))
FROM `user`
WHERE `user`.`user_status` = 'A';

It will return the result that i want if i run this

SELECT `user_id`, (SELECT calculate_marks(`user_id`))
FROM `user`
WHERE `user`.`user_status` = 'A';
3
  • This is correct syntax for an INSERT INTO operation INSERT INTO table_name (column1, column2, column3) VALUES (value1, value2, value3); You are missing out the VALUES part Commented Oct 11, 2018 at 6:09
  • 2
    @pravinnavle you are wrong! OP is using Insert.. Select statement. Refer: dev.mysql.com/doc/refman/8.0/en/insert-select.html Commented Oct 11, 2018 at 6:11
  • 1
    @MadhurBhaiya thanks for pointing out my mistake. Got to learn something new about MySQL Commented Oct 11, 2018 at 6:16

1 Answer 1

2

You are trying a correlated subquery, without proper aliasing. You dont need to use subquery to calculate marks. Try the following instead:

INSERT INTO `marks`
(`marks_user_id`, `marks_addtime`, `marks_amount`
) 
SELECT `user_id`, UNIX_TIMESTAMP(), calculate_marks(`user_id`) 
FROM `user`
WHERE `user`.`user_status` = 'A';
Sign up to request clarification or add additional context in comments.

Comments

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.