Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6137059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:40:47+00:00 2026-05-23T17:40:47+00:00

I am writing a trigger that will execute everytime a student completes an exam

  • 0

I am writing a trigger that will execute everytime a student completes an exam assignment and will update a table with what their current rank is amongst their fellow students (highest marks gives rank of 1, etc)

I started with the following query to see if I would get usable data from my student exam submissions

SET @rownum := 0;
SET @school := 'hillview';
SELECT userID, rank, firstname, institution
FROM (
SELECT @rownum := @rownum + 1 AS rank, userID, firstname, institution FROM(
    SELECT userID, sum(marks), firstname, institution
    FROM competition_history
    WHERE institution = @school
    GROUP BY userID
    ORDER BY marks DESC) as inner_rank
) as r
WHERE userID = 2;

Which returned a correct result of

++++++++++++++++++++++++
uid|rank|fname |school
++++++++++++++++++++++++
2 | 2 |stefan|hillview

So my student with the userID = 2 is ranked second, great, now I want to convert this into a trigger and write it to a table that stores just userID and rank

I tried to convert this to a trigger (and in so removing the hardcoded institution variable)

DELIMITER $$
DROP TRIGGER IF EXISTS CallRankProc;
CREATE TRIGGER callRankProc AFTER INSERT
ON competition_history
FOR EACH ROW
BEGIN
DECLARE rownum INTEGER DEFAULT 0;
DECLARE userRank INTEGER;
SELECT _rank into userRank
FROM (
SELECT rownum = rownum + 1 AS _rank, userID, firstname, institution FROM(
    SELECT userID, sum(marks), firstname, institution
    FROM competition_history
    WHERE institution = NEW.institution
    GROUP BY userID
    ORDER BY marks DESC) as inner_rank
) as r
WHERE userID = NEW.userID;

INSERT INTO `student_ranks`(`userID`,`rank`) VALUES (NEW.userID,userRank)
ON DUPLICATE KEY UPDATE rank = userRank;

END$$

However, this inserts into my table
+++++++++++
uid| rank |
+++++++++++
2 | 0 |

Now since it inserted uid = 2 I believe it is pulling the correct values, and I can’t seem to figure out why then the userRank is 0, meaning that rownum is not incrementing.

I am trying to pinpoint why the trigger code returns the default variable value and if it is based on the nested select. Can anyone pinpoint the problem? Can you have nested selects in MySQL triggers? When I added the trigger no errors were thrown.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:40:47+00:00Added an answer on May 23, 2026 at 5:40 pm

    Try to set rownum to a default value

    DECLARE rownum INTEGER DEFAULT 0
    

    in your current trigger, the following expression

    rownum = rownum + 1

    will evaluate to NULL, due to the default value of rownum being null.

    Additionally the = operator checks for equality (as in “x is equal to y”) which means the expression will evaluate to true or false. And since rownum will never be equal to rownum+1 , your result will be false (this is why you are seeing a 0 in your resultset).

    EDIT
    Try SELECT rownum+1 INTO rownum

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a trigger that will do a cross table update, but I can't
I need to write an Insert, Update Trigger on table A which will delete
I am using INSERT Trigger on that table. Once trigger is executed (it update
I am interested in writing a trigger that would ignore queries from a specific
I am using SQL Server 2000. I am writing a trigger that is executed
When writing a row-level trigger in Oracle, I know that you can use the
I am currently writing a script that will basically ‘install’ a codeigniter installation automatically.
Currently I'm writing a utility application that will connect to a given IP and
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing documentation in html requires some code examples. What to do with characters that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.