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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:58:20+00:00 2026-05-26T13:58:20+00:00

I have written a MySQL query (Ad rotate algorithm) to fetch records from multiple

  • 0

I have written a MySQL query (Ad rotate algorithm) to fetch records from multiple tables.

select Q2.*
from User u,(
    select 
        a.adId as "AdId",
        a.UserId as "UserId",
        a.Title as "Title",
        a.ImageURL as "ImageURL",
        a.RefURL as "RefURL",
        a.CreateDate as "CreateDate",
        a.StartDate as "StartDate",
        a.RunTill as "RunTill",
        a.Budget as "Budget",
        a.Status as "Status",
        (a.budget - COALESCE(Q1.A2,0)) as "Remaining"
    from Ad a
    LEFT JOIN(
        select
            AdId as A1,
            count(*) as A2
        from Referral 
        where date(ReferralDate)=date(CURRENT_TIMESTAMP) 
        group by AdId
    ) as Q1
    ON a.AdId = Q1.A1
    and a.StartDate<CURRENT_TIMESTAMP
    and a.RunTill>CURRENT_TIMESTAMP
    and a.Status = 1
) as Q2 
where u.Authorized = true
and u.Balance>1
and u.UserId = Q2.UserId
order by Q2.Remaining desc;

The above query has a filter a.Status = 1, but in the result, i’m getting rows with Status != 1 as well. Resultset below:

+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------
------------+---------------------+---------------------+--------+--------+-----------+
| AdId         | UserId       | Title                          | ImageURL                                                       | RefURL                         | CreateDa
te          | StartDate           | RunTill             | Budget | Status | Remaining |
+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------
------------+---------------------+---------------------+--------+--------+-----------+
| 382944516829 | 724511865288 | Online Advertising for Nepal   | /static/image/adimage/noimage.jpg                              | http://www.nepaladz.com        | 2011-11-
03 13:47:47 | 2011-11-03 00:00:00 | 2011-11-30 00:00:00 |    100 |      0 |       100 |
| 973252821643 | 724511865288 | Models, news, fashion and more | http://nepalads.servehttp.com:8080/static/image/adimage/7.jpg  | http://www.cybersansar.com     | 2011-10-
18 15:57:49 | 2011-11-03 10:59:57 | 2011-11-18 15:57:49 |     70 |      1 |        70 |
| 805400799468 | 724511865288 | Alibaba market                 | http://nepalads.servehttp.com:8080/static/image/adimage/4.jpg  | http://www.alibaba.com         | 2011-10-
18 15:54:42 | 2011-11-03 10:59:57 | 2011-11-18 15:54:42 |     60 |      1 |        60 |
| 333179565363 | 724511865288 | Nepal AT&T Network             | http://nepalads.servehttp.com:8080/static/image/adimage/3.jpg  | http://www.att.com             | 2011-10-
18 15:54:00 | 2011-11-03 10:59:57 | 2011-11-18 15:54:00 |     60 |      1 |        60 |
| 576540783739 | 724511865288 | Travel with us!                | http://nepalads.servehttp.com:8080/static/image/adimage/8.jpg  | http://www.manang.com          | 2011-10-
18 15:58:39 | 2011-11-03 10:59:57 | 2011-11-18 15:58:39 |     45 |      1 |        43 |
| 011731192504 | 724511865288 | Nepal Online Shopping          | http://nepalads.servehttp.com:8080/static/image/adimage/11.jpg | http://www.harilo.com          | 2011-10-
18 16:02:32 | 2011-11-03 10:59:57 | 2011-11-18 16:02:32 |     45 |      1 |        42 |
| 232044045570 | 724511865288 | Himalayan Java                 | http://nepalads.servehttp.com:8080/static/image/adimage/1.jpg  | http://www.himalayanjava.com   | 2011-10-
18 15:51:34 | 2011-11-03 10:59:57 | 2011-11-18 15:51:34 |     30 |      1 |        30 |
| 471978035014 | 724511865288 | Home TV. 50% discount          | http://nepalads.servehttp.com:8080/static/image/adimage/5.jpg  | http://www.dishhome.com.np     | 2011-10-
18 15:56:03 | 2011-11-03 10:59:57 | 2011-11-18 15:56:03 |     30 |      1 |        30 |
| 543726500808 | 724511865288 | Live the adventure             | http://nepalads.servehttp.com:8080/static/image/adimage/9.jpg  | http://www.adventuresnepal.com | 2011-10-
18 15:59:21 | 2011-11-03 10:59:57 | 2011-11-18 15:59:21 |     25 |      1 |        25 |
| 757765466809 | 724511865288 | Wanna meet me? Click here      | http://nepalads.servehttp.com:8080/static/image/adimage/10.jpg | http://www.missnepal.com.np    | 2011-10-
18 16:00:14 | 2011-11-03 10:59:57 | 2011-11-18 16:00:14 |     25 |      1 |        23 |
| 890639256469 | 724511865288 | Learn dance from Gurus         | http://nepalads.servehttp.com:8080/static/image/adimage/6.jpg  | http://www.salsanepal.com      | 2011-10-
18 15:56:45 | 2011-11-03 10:59:57 | 2011-11-18 15:56:45 |     15 |      1 |        15 |
| 838481835983 | 724511865288 | Fashionista Nepal              | http://nepalads.servehttp.com:8080/static/image/adimage/2.jpg  | http://www.nepalfashion.com    | 2011-10-
18 15:53:06 | 2011-11-03 10:59:57 | 2011-11-18 15:53:06 |     15 |      1 |        14 |
+--------------+--------------+--------------------------------+----------------------------------------------------------------+--------------------------------+---------

How can I fix this?

Thanks in advance.
James

  • 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-26T13:58:20+00:00Added an answer on May 26, 2026 at 1:58 pm

    You should move and a.Status = 1 to a where clause:

    select Q2.*
    from User u,(
        select 
            a.adId as "AdId",
            a.UserId as "UserId",
            a.Title as "Title",
            a.ImageURL as "ImageURL",
            a.RefURL as "RefURL",
            a.CreateDate as "CreateDate",
            a.StartDate as "StartDate",
            a.RunTill as "RunTill",
            a.Budget as "Budget",
            a.Status as "Status",
            (a.budget - COALESCE(Q1.A2,0)) as "Remaining"
        from Ad a
        LEFT JOIN(
            select
                AdId as A1,
                count(*) as A2
            from Referral 
            where date(ReferralDate)=date(CURRENT_TIMESTAMP) 
            group by AdId
        ) as Q1
        ON a.AdId = Q1.A1
        WHERE
            a.StartDate<CURRENT_TIMESTAMP
        and a.RunTill>CURRENT_TIMESTAMP
        and a.Status = 1
    ) as Q2 
    where u.Authorized = true
    and u.Balance>1
    and u.UserId = Q2.UserId
    order by Q2.Remaining desc;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this query for retrieving data from mysql as below select FeedbackCode,EMailID,FeedbackDetail,
I have written a query I want to run against a MySQL db every
We have a web application written in PHP that exports MySQL query results to
I have written a script to query a mysql database and encode the data
I have an AJAX script that receives a string from a mySQL query returned
I have written a small chat app that uses mysql + php to facilitate
I have written an AIR Application that downloads videos and documents from a server.
Hi i'm struggling to write a particular MySQL Join Query. I have a table
I have written a simple mysql search form that searches between two dates. This
I have written a query which will perform Full Text search using full search

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.