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

  • Home
  • SEARCH
  • 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 104135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:14:02+00:00 2026-05-11T01:14:02+00:00

I have the below query, which basically it retrieves the 5 top most books

  • 0

I have the below query, which basically it retrieves the 5 top most books sold:

    select top 5 count(id_book_orddetails) 'books_sold', bk.*     from orderdetails_orddetails ord inner join books_book bk     on ord.id_book_orddetails = bk.id_book     group by id_book, name_book,author_book,desc_book,id_ctg_book,qty_book,image_book,isdeleted      order by 'books_sold' desc  

The problem is that I am receiving this error:

The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.

In the books_book table, the field desc_book is of type ntext, and I’m sure that the problem is coming from there.

This is because before I changed the desc_book to ntext, it was of type nvarchar and it worked perfectly.

The reason I changed the data type of this field is because somehow in PHP website, when I was displaying the book description (a different sp), the description was being truncated to about 200-255 characters, thus I changed it to ntext and it ‘solved my problem’ (ie, the whole desc_book was finally being displayed).

So basically these are my questions :

  1. Why is the desc_book (nvarchar) field being truncated when displayed in a PHP page?
  2. How can i fix the SQL query to accommodate for grouping by an ntext field?

Just for the record (which I don’t think is very relevant), I am using MS SQL Server 2005

[UPDATE]

I tried and tested both of Bill Karwin‘s proposed solutions and they both work perfectly. I thus decided in grouping the count aggregate result into a subquery…ie’s Karwin’s latter solution.

So here is my updated (fully working) statement:

SELECT bk.*, bc.books_sold FROM books_book bk INNER JOIN (     SELECT bk2.id_book, COUNT(*) books_sold     FROM books_book bk2      INNER JOIN orderdetails_orddetails ord      ON (bk2.id_book = ord.id_book_orddetails)     GROUP BY bk2.id_book ) bc ON (bk.id_book = bc.id_book) ORDER BY books_sold desc; 
  • 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. 2026-05-11T01:14:02+00:00Added an answer on May 11, 2026 at 1:14 am

    The old PHP ‘mssql’ extension only supports VARCHAR up to 255 bytes in size. This is a known limitation, and it’s why Microsoft has been developing a new PHP extension to support modern SQL Server releases.

    One workaround is to declare the storage of that column as NVARCHAR, but when you query it from PHP, use CAST to convert it to NTEXT. Then the full length can be returned.

    Another option is to keep the column stored as NTEXT, but GROUP BY only book_id by putting the count into a subquery:

    SELECT bk.*, bc.books_sold FROM books_book bk   INNER JOIN (SELECT bk2.book_id, COUNT(*) books_sold       FROM books_book bk2 INNER JOIN orderdetails_orddetails ord          ON (bk2.id_book = ord.id_book_orddetails)       GROUP BY bk2.book_id) bc     ON (bk.book_id = bc.book_id); 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 78k
  • Answers 78k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer No, the setting is in IIS when selecting application pool… May 11, 2026 at 3:45 pm
  • added an answer You could take a look at Simian or DuplicateFinder. Neither… May 11, 2026 at 3:44 pm
  • added an answer NDepend is pretty good at this. Additionally Visual Studio 2008… May 11, 2026 at 3:44 pm

Related Questions

I need to make a rather complex query, and I need help bad. Below
I'm a Java programmer, but not a JavaScript programmer. I have just discovered YUI,
I currently have the query running on Postgres: SELECT * FROM addenda.users WHERE users.username
Along the same vein as the questions asked yesterday, I have another count if

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.