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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:56:20+00:00 2026-06-03T22:56:20+00:00

I have this query and result in my database: mysql> SELECT id FROM jos_content

  • 0

I have this query and result in my database:

mysql> SELECT id FROM jos_content 
       WHERE catid=12 AND state=1 
       AND DATE(publish_up) <= NOW() ORDER BY DATE(publish_up) DESC LIMIT 0,5;
    +----+
    | id |
    +----+
    | 48 |
    | 47 |
    | 46 |
    +----+
    3 rows in set (0.00 sec)

Now I would like to count result:

mysql> SELECT count(*), id FROM jos_content 
       WHERE catid=12 AND state=1 
       AND DATE(publish_up) <= NOW() ORDER BY DATE(publish_up) DESC LIMIT 0,5;
    +----------+----+
    | count(*) | id |
    +----------+----+
    |        3 | 46 |
    +----------+----+
    1 row in set (0.00 sec)

Why aren’t the 48 and 47 results shown?

Thank you in advance

  • 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-03T22:56:22+00:00Added an answer on June 3, 2026 at 10:56 pm

    You need to include a GROUP BY clause. By default, MySQL permits columns in the SELECT clause which are not also included in a GROUP BY (which is an error in most other RDBMS). Without the GROUP BY, MySQL will return the first of the associated columns, which is often not the correct intended result.

    SELECT 
      count(*), 
      id
    FROM jos_content
    WHERE catid=12 AND state=1 AND DATE(publish_up) <= NOW() 
    GROUP BY id
    ORDER BY DATE(publish_up) DESC
    LIMIT 0,5;
    

    Not requiring all columns in the GROUP BY is helpful if you want to return multiple columns in a summary which are always unique, as something like the following, where name is unique to id, but you MUST group at least one column for an aggregate to act correctly and produce a summary:

    SELECT 
      id,
      name,
      COUNT(*)
    FROM tbl
    GROUP BY id
    

    For compatibility with other RDBMS, and to avoid bugs, I recommend always including all the SELECT columns in the GROUP BY explicitly.

    Update

    With better understanding of what you attempted, if you want the total count of all rows beside the id of each row, you can use a subselect like in the SELECT list. This is going to be a bit slow though.

    SELECT
      id,
      (SELECT COUNT(*) FROM jos_content) AS numitems
    FROM jos_content
    WHERE catid=12 AND state=1 AND DATE(publish_up) <= NOW() 
    ORDER BY DATE(publish_up) DESC
    LIMIT 0,5;
    

    You can trick this to work a little faster by doing a cartesian join (no ON clause) against the count in a subquery, so it shouldn’t get executed for each row. Only do this if the subquery will return exactly one row.

    SELECT
      id
      numitems
    FROM 
      jos_content
      /* Join with no ON clause */
      JOIN (SELECT COUNT(*) AS numitems FROM jos_content) subq
    WHERE catid=12 AND state=1 AND DATE(publish_up) <= NOW() 
    ORDER BY DATE(publish_up) DESC
    LIMIT 0,5;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: $result = mysql_query(SELECT * FROM quote ORDER BY RAND() LIMIT
I have this query which returns a result set with Date(column) as nvarchar datatype.
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have a query to filter the result in mysql database into given letter
I have this query intended for reporting off of a MySQL database. The query
Hi I have this Query and I'm getting an empty result, Anyone see anything
I have a Linq to Entities query like this one: var results = from
I have this query: (from r in gServiceContext.CreateQuery(opportunity) join c in gServiceContext.CreateQuery(contact) on ((EntityReference)r[new_contact]).Id
I have this query which is running perfectly From this query I am selecting
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted

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.