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 7507873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:34:24+00:00 2026-05-29T22:34:24+00:00

Description I’m trying to JOIN a table using the result of a stored function.

  • 0

Description

I’m trying to JOIN a table using the result of a stored function. The stored function returns an unsigned integer, which is the same type as the field I’m joining against.

Problem

The field I’m trying to JOIN to is a primary key. My issue is that MySQL doesn’t seem to be using the primary key here.

The relevant lines of the query (shown below) are:

   LEFT JOIN second_db.s
     ON s.id = first_db.find_spend_id(qi.other_type, qi.other_id)

If I replace my function call first_db.find_spend_id(...) with an integer, for example 30, EXPLAIN shows that the primary key is used, and the query is fast. If I use my function, EXPLAIN shows that the primary key is not used, and the query is slow.

Question

Is there some way I can use my function and have MySQL use the primary key when using the result of this function in this join? How should I modify my code in order to attain this most glorious resolution?

Information

Query

EXPLAIN SELECT *
FROM   first_db.qhr
   JOIN first_db.qi
     ON qi.id = qhr.issue_id
        AND qhr.status = 'Accepted'
   LEFT JOIN second_db.s
     ON s.id = first_db.find_spend_id(qi.other_type, qi.other_id)
   JOIN second_db.spend_reports AS sr
     ON sr.id = s.spend_report_id
   JOIN first_db.companies AS b
     ON b.id = s.member_id
   JOIN first_db.brands
     ON brands.id = sr.brand_id
LIMIT  0, 10  

Function

CREATE DEFINER=`user`@`%` FUNCTION `find_spend_id`(lookup_other_type ENUM('One', 'Two'), lookup_other_id INT(10)) 
    RETURNS int(10)
    READS SQL DATA
    BEGIN
        (...snip actual code...)
        return 30;
    END

Explain

id   select_type    table    type    possible_keys    key    key_len    ref    rows    Extra
(...snip...)
1    SIMPLE         s        ALL      NULL            NULL   NULL       NULL   5999    "Using join buffer"
(...snip...)
  • 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-29T22:34:25+00:00Added an answer on May 29, 2026 at 10:34 pm

    In my case, what I wanted to achieve was not possible with this approach.

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

Sidebar

Related Questions

Problem description: I wanted a delete function which could perform delete/remove of the selected
Problem Description I'm using Expat with a custom C++ wrapper, which I already tested
Description of LEN() function on MSDN : Returns the number of characters of the
The description of System.Collection.Specialized.HybridDictionary is this: Implements IDictionary by using a System.Collections.Specialized.ListDictionary while the
The description of CoLoadLibrary() says it does pretty much the same as LoadLibraryEx() -
I am trying to understand how to use SyndicationItem to display feed which is
Description This is not a real world example! Please don't suggest using decimal or
Description I'm trying to test application coded in Delphi (VCL components by DevEx) with
Description: I have a column(EmailsAdress) on a table(BusinessUsers) on my databases that stores email
Description Resource Path Location Type No grammar constraints (DTD or XML schema) detected for

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.