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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:41:00+00:00 2026-05-26T03:41:00+00:00

I’m trying to retrieve the closest adjacent rows (above and below) a record in

  • 0

I’m trying to retrieve the closest adjacent rows (above and below) a record in a result set that match a certain condition.

My problem appears to be in my use of min and max here:

WHERE   ma_c.id = 2 
            AND ma_e.id > 4 
            AND ma_e.id = MIN(ma_e.id))

The error I receive is:
error : Invalid use of group function

Not quite sure what I’m doing incorrectly

    (SELECT     
        ma_c.id                 as contest_id,
        ma_c.name               as name,
        ma_c.title              as title,
        ma_u.id                 as user_id,
        ma_u.first_name         as user_first_name,
        ma_u.last_name          as user_last_name,
        ma_u.street_address     as user_street_address,
        ma_u.city               as user_city,
        ma_u.zip_code           as user_zip_code,
        ma_u.email              as user_email,
        ma_u.phone_number       as user_phone_number,
        ma_u.country_code       as user_country_code,
        ma_u.gender             as user_gender,
        ma_u.dob                as user_dob,
        ma_u.ssn                as user_ssn,
        ma_u.canadian_province  as user_canadian_province,
        ma_u.state              as user_state,
        ma_e.id                 as entry_id,
        ma_e.hash_id            as entry_hash_id,
        ma_e.create_datetime    as entry_create_datetime,
        ma_e.entry_type         as entry_type,
        ma_e.title              as entry_title
    FROM    ma_contests ma_c
    JOIN    ma_users ma_u
    ON      ma_c.id = ma_u.contests_id

    JOIN    ma_entries ma_e
    ON      ma_u.id = ma_e.users_id

    WHERE   ma_c.id = 2 
            AND ma_e.id > 4 
            AND ma_e.id = MIN(ma_e.id))

UNION

    (SELECT     
        ma_c.id                 as contest_id,
        ma_c.name               as name,
        ma_c.title              as title,
        ma_u.id                 as user_id,
        ma_u.first_name         as user_first_name,
        ma_u.last_name          as user_last_name,
        ma_u.street_address     as user_street_address,
        ma_u.city               as user_city,
        ma_u.zip_code           as user_zip_code,
        ma_u.email              as user_email,
        ma_u.phone_number       as user_phone_number,
        ma_u.country_code       as user_country_code,
        ma_u.gender             as user_gender,
        ma_u.dob                as user_dob,
        ma_u.ssn                as user_ssn,
        ma_u.canadian_province  as user_canadian_province,
        ma_u.state              as user_state,
        ma_e.id                 as entry_id,
        ma_e.hash_id            as entry_hash_id,
        ma_e.create_datetime    as entry_create_datetime,
        ma_e.entry_type         as entry_type,
        ma_e.title              as entry_title
    FROM    ma_contests ma_c
    JOIN    ma_users ma_u
    ON      ma_c.id = ma_u.contests_id

    JOIN    ma_entries ma_e
    ON      ma_u.id = ma_e.users_id

    WHERE   ma_c.id = 2 
            AND ma_e.id < 4 
            AND ma_e.id = MAX(ma_e.id))

—–EDIT—-
This is the relevant schema uml for the query I’m attempting to execute
enter image description here

  • 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-26T03:41:00+00:00Added an answer on May 26, 2026 at 3:41 am
    SELECT     
            ma_c.id                 as contest_id,
            ma_c.name               as name,
            ma_c.title              as title,
            ma_u.id                 as user_id,
            ma_u.first_name         as user_first_name,
            ma_u.last_name          as user_last_name,
            ma_u.street_address     as user_street_address,
            ma_u.city               as user_city,
            ma_u.zip_code           as user_zip_code,
            ma_u.email              as user_email,
            ma_u.phone_number       as user_phone_number,
            ma_u.country_code       as user_country_code,
            ma_u.gender             as user_gender,
            ma_u.dob                as user_dob,
            ma_u.ssn                as user_ssn,
            ma_u.canadian_province  as user_canadian_province,
            ma_u.state              as user_state,
            ma_e.id                 as entry_id,
            ma_e.hash_id            as entry_hash_id,
            ma_e.create_datetime    as entry_create_datetime,
            ma_e.entry_type         as entry_type,
            ma_e.title              as entry_title
        FROM    ma_contests ma_c
        JOIN    ma_users ma_u
        ON      ma_c.id = ma_u.contests_id
    
        JOIN    ma_entries ma_e
        ON      ma_u.id = ma_e.users_id
    
        WHERE   ma_c.id = 2 
                AND ma_e.id > 4 
                AND ma_e.id = (SELECT min(_ma_e.id)       
                                FROM    ma_contests _ma_c
                                JOIN    ma_users _ma_u
                                ON      _ma_c.id = _ma_u.contests_id
    
                                JOIN    ma_entries _ma_e
                                ON      _ma_u.id = _ma_e.users_id
    
                                WHERE   _ma_c.id = 2 
                                        AND _ma_e.id > 4)
    

    and same for the next query

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:

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.