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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:29:13+00:00 2026-05-23T16:29:13+00:00

Pulling some coupons from a database. Each coupon has a merchantid column that contains

  • 0

Pulling some coupons from a database. Each coupon has a merchantid column that contains the id for the merchant for which the coupon belongs too.

I’m trying to construct a query that pulls 5 coupons, but I only want 1 coupon per merchantid. I don’t want multiple coupons with the same merchantid.

I have some WHERE conditions that need to go in there as well. This what I have so far,its pulling only 1 coupon per merchant, but the coupon its pulling doesn’t have the highest clicks:

SELECT C.couponid,C.fmtc_couponid,C.merchantid,
       C.label,C.restrictions,C.coupon,C.link,
       C‌.image,C.expire,C.unknown,C.clicks,M.name,
       M.approved,M.homepageurl,M.logo_image,M‌.permalink
FROM tblCoupons C,tblMerchants M
WHERE C.merchantid=M.merchantid AND
      C.begin < ".mktime()." AND
      C.expire > ".mktime()." AND
      M.display='1'
GROUP BY C.merchantid ORDER BY C.clicks DESC LIMIT 0,5
  • 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-23T16:29:14+00:00Added an answer on May 23, 2026 at 4:29 pm

    I think what you need is to pre-query from the coupons table, group by the merchant, then re-join back.

    select 
          PreQuery.merchantID,
          C2.couponid,
          C2.fmtc_couponid,
          C2.merchantid,
          C2.label,
          C2.restrictions,
          C2.coupon,
          C2.link,
          C‌2.image,
          C2.expire,
          C2.unknown,
          C2.clicks,
          M.name,
          M.approved,
          M.homepageurl,
          M.logo_image,
          M‌.permalink
       from 
          ( select c.merchantid,
                   max( c.clicks ) as HiClickCoupon
               from
                  tblCoupons c
                  join tblMerchants M1
                     on PreQuery.MerchantID = M1.MerchantID
                     AND M1.DIsplay = '1'
               where
                      c.begin < ".mktime()."
                  and c.expire > ".mktime()."
               group by 
                  c.merchantid
               order by 
                  max( c.Clicks ) DESC
               limit 
                  0, 5 ) PreQuery
    
          join tblMerchants M
             on PreQuery.MerchantID = M.MerchantID
    
          join tblCoupons C2
             on PreQuery.MerchantID = C2.MerchantID
             AND PreQuery.HiClickCoupon = C2.Clicks
             AND C2.begin < ".mktime()."
             AND C2.expire > ".mktime()."
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pulling some text from a database, which contains some carriage returns. When I
I am pulling in some information from a database that contains dimensions with both
I'm pulling some text passages from a database that have some minimal formatting using
I have some strings that I am pulling out of a database and I
I'm pulling some info from a database then putting it into a DIV and
I have two functions that pulling some of content from html and returning it
I am currently replacing some functionality in a twiki page that has been pulling
I'm pulling some internationalized text from a MS SQL Server 2005 database. As per
I'm pulling some RSS feeds in from YouTube which have invalid UTF8. I can
I am pulling some data from mysql database using PHP, now I would like

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.