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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:41:21+00:00 2026-06-06T12:41:21+00:00

i have an question how to use an alias column at the where/having clause.

  • 0

i have an question how to use an “alias” column at the where/having clause. I know there are some question with an related topic. But I search trough the web and on stackoverfolw and doesn’t find an solution.

So i hope for help at this way..

The following statement works well:

SELECT PHRASE,COUNT(*) AS A 
    FROM my_statistics 
    WHERE TIMESTAMP>NOW()-INTERVAL 7 DAY
    GROUP BY PHRASE ORDER BY A DESC 
    LIMIT 16;

PHRASE and TIMESTAMPare columns at the table.
The code selects the top 16 PHRASES, which are inserts by users on the last 7 days.
There are also exist an column USER and so i like now to select the top 16 phrases which are inserted by more than one user.
So i tried this:

SELECT PHRASE,COUNT(*) AS A, COUNT(DISTINCT(USER)) AS B
    FROM my_statistics
    WHERE TIMESTAMP>NOW()-INTERVAL 7 DAY
          AND B>1
    GROUP BY PHRASE ORDER BY A DESC 
    LIMIT 16;

On other questions on stackoverflow i fond the info, that i have to use HAVING

SELECT PHRASE,COUNT(*) AS A, COUNT(DISTINCT(USER)) AS B
    FROM my_statistics
    WHERE TIMESTAMP>NOW()-INTERVAL 7 DAY      
    GROUP BY PHRASE ORDER BY A DESC 
    HAVING B>1
    LIMIT 16;

But this returns an error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘HAVING B>1
LIMIT 16’ at line 5

I have no idea, how the right syntax could be.
Hope for any kind of help here.
Thank you!

  • 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-06T12:41:22+00:00Added an answer on June 6, 2026 at 12:41 pm

    Place the ORDER BY A DESC after Having clause.

    SELECT phrase, 
           Count(*)                AS A, 
           Count(DISTINCT( user )) AS B 
    FROM   my_statistics 
    WHERE  timestamp > Now() - INTERVAL 7 day 
    GROUP  BY phrase 
    HAVING b > 1 
    ORDER  BY a DESC 
    LIMIT  16; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a question how to use an alias from a select query: $timecheck
I have a question but I'm not sure of the word to use. My
I have question regarding the use of function parameters. In the past I have
I have a question regarding use of FFT. Using function getBand(int i) with Minim
I have a question about the use of multidimensional std::intializer_list in C++. I have
I have a question about the use of data structures such as ArrayLists in
I have a question that can I use AS400ConnectionPool in Spring if then pls
I have a question regarding VB.net and the use of rsData connections to an
I have a simple question: if I use URLConnection.setUseCaches(true) when loading something from web,
I have a question, which Unicode encoding to use while encoding .NET string into

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.