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

  • Home
  • SEARCH
  • 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 9194781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:29:38+00:00 2026-06-17T21:29:38+00:00

I am having quite a frustrating problem with a stored function in MySQL. I

  • 0

I am having quite a frustrating problem with a stored function in MySQL.
I have a database full of session data from users connecting to a wireless hotspot. I am trying to select individual user download statistics from within a selected month. My problem is that the subquery within the function seems to ignore the mac field in the WHERE statement. Here is my code:

CREATE FUNCTION get_month_download(mo varchar(45), box int(11), mac varchar(45))   RETURNS DOUBLE
BEGIN
    DECLARE dwnld double;
    IF mo IS NULL THEN
        SET mo := CONCAT(CONCAT(YEAR(NOW()), '-', MONTH(NOW())),'-','01');
    END IF;
    SET dwnld := (
        SELECT SUM(`tx_bytes`) 
        FROM `session` 
        WHERE `assoc_time` > UNIX_TIMESTAMP(mo) 
        AND `disassoc_time` < UNIX_TIMESTAMP(DATE_ADD(mo, INTERVAL 1 MONTH))
        AND `mac` = mac
        AND `controller_id` = box
    );
    return dwnld;
END

Running this:

SELECT get_month_download('2012-09-01', '2', '00:21:5c:56:be:a3');

Returns download data for the entire table, though it is using the controller_id to filter the data.

If I run the subquery outside of the function using the same parameters, it works fine. What gives?

To be more clear, running this query:

SELECT SUM(`tx_bytes`) 
FROM `session` 
WHERE `assoc_time` > UNIX_TIMESTAMP('2012-09-01') 
AND `disassoc_time` < UNIX_TIMESTAMP(DATE_ADD('2012-09-01', INTERVAL 1 MONTH))
AND `mac` = '00:21:5c:56:be:a3'
AND `controller_id` = '2';

returns the correct download statistic for that user. Where the function returns the statistic for all users of that controller.

  • 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-06-17T21:29:39+00:00Added an answer on June 17, 2026 at 9:29 pm

    I should really reflect back to what my teachers taught me in college more often. The issue was that the variable mac, in the scope of the select statement, was seen as a field from the table, and not as my parameter from the function. So, changing the parameter name fixed the issue:

    CREATE FUNCTION get_month_download(mo varchar(45), box int(11), inmac varchar(60)) RETURNS DOUBLE 
    BEGIN  
        DECLARE dwnld double;
        IF mo IS NULL THEN
            SET mo := CONCAT(CONCAT(YEAR(NOW()), '-', MONTH(NOW())),'-','01');
        END IF;
        SET dwnld := (
            SELECT SUM(`tx_bytes`)   
            FROM `session`    
            WHERE `assoc_time` > UNIX_TIMESTAMP(mo)    
            AND `disassoc_time` < UNIX_TIMESTAMP(DATE_ADD(mo, INTERVAL 1 MONTH))      
            AND `controller_id` = box
            AND `mac` = inmac
        );
         return dwnld; 
    END
    

    This has been a ‘smack in the face’ moment. Thank you all for your help.

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

Sidebar

Related Questions

I'm having quite a weird problem here. I have just pasted the Yii folder
I'm having quite a frustrating problem that I'm sure is TRIVIAL. Whenever I move
I'm quite new to Amazon S3 and I'm having difficulty downloading large files from
I'm quite new to OpenId and I'm having a bit of a problem understanding
I'm quite new to sql and I'm having trouble writing this i have a
Have looked quite hard for this answer but having no luck. I have 3
I am having quite a tricky problem, which i just cannot seem to solve.
I'm having quite a problem deciding how to serve a few Python scripts. The
I'm having quite a challenge... I have a web page where the user clicks
I'm having quite a bit of problem deciding how to represent a certain problem.

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.