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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:09:44+00:00 2026-05-22T13:09:44+00:00

I have a fairly complex query in SQL that performs a count across two

  • 0

I have a fairly complex query in SQL that performs a count across two tables. The query should look like this:

SELECT 
((SELECT COUNT(DISTINCT(pages.id)) AS `count` 
    FROM `pages` 
    INNER JOIN `pageRegions` ON pageRegions.pageId = pages.id 
    WHERE (MATCH (pages.name, pageRegions.contents) AGAINST ('+keyword*' IN BOOLEAN MODE))) + 
(SELECT COUNT(documents.id) AS `count` 
    FROM `documents` 
    INNER JOIN `files` ON files.id = documents.file 
    WHERE (MATCH (documents.name, 
              files.name, 
              files.extracted_text) AGAINST ('+keyword*' IN BOOLEAN MODE)))) AS count

Unfortunately when I have the following code using Zend_Db the query includes a load of additional columns so the addition of both queries obviously fails:

$total_count_select = $PagesTable->getAdapter()
    ->query('((' . $pages_total_count_select . ') + 
              (' . $legal_resources_total_count_select . ')
             ) AS count');

Where $pages_total_count_select and $legal_resources_total_count_select are both Zend_Db_Select objects.

I have tried using the columns() method to specify the columns I want on each select object like so:

$legal_resources_total_count_select->columns('COUNT(documents.id) AS count');

But this just appears to append onto the select query rather than only returning the column specified.

So the Zend generated query ends up looking like this:

SELECT ((SELECT `pages`.*, 
                 ((1.3 * (MATCH(pages.name) AGAINST ('+keyword*' IN BOOLEAN MODE))) + 
                       (0.8 * (MATCH(pageRegions.contents) AGAINST ('+keyword*' IN BOOLEAN MODE)))) AS `score`, 
                     `pages`.`name` AS `page_name`, 
                     `pages`.`id` AS `page_id`, 
                     `pageRegions`.*, 
                     COUNT(DISTINCT(pages.id)) AS `count`
         FROM `pages`
         INNER JOIN `pageRegions` ON pageRegions.pageId = pages.id
         WHERE (MATCH (pages.name, pageRegions.contents) AGAINST ('+keyword*' IN BOOLEAN MODE))
         ORDER BY `score` DESC)

            + 

          (SELECT `documents`.*,
                   ((1.3 * (MATCH(documents.title) AGAINST ('+keyword*' IN BOOLEAN MODE))) + 
                       (0.8 * (MATCH(documents.short_description, files.NAME, files.extracted_text) AGAINST ('+keyword*' IN BOOLEAN MODE)))) AS `score`,
                     `files`.*, 
                    COUNT(documents.id) AS `count`
         FROM `documents`
         INNER JOIN `files` ON files.id = documents.file
         WHERE (MATCH (documents.title, documents.short_description, files.name, files.extracted_text) AGAINST ('+keyword*' IN BOOLEAN MODE))
         ORDER BY `score` DESC)
        ) AS COUNT

How can I get rid of all the extra columns it is selecting?

  • 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-22T13:09:44+00:00Added an answer on May 22, 2026 at 1:09 pm

    You forgot one part of your code, where you construct the $pages_total_count_select and $legal_resources_total_count_select.

    You missed one parameter in the way to build these, which is the array of columns you want, if you do not give it it takes table.*.

    So just check the way you create the select and add one argument with an empty array.

    UPDATE:
    So you do:

    $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
    

    Just after that redo a ->from() call with your Zend_Db_Table ($this here) here you can specify the columns, so an empty array or the only one you want:

    $select = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
    $select->from($this, array(new Zend_Db_Expr('COUNT(documents.id) AS count')));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.