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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:40:00+00:00 2026-05-24T23:40:00+00:00

This feels like it should be really easy, but I just can’t get it

  • 0

This feels like it should be really easy, but I just can’t get it and I’ve tried everything.

How can I just get the NUMBER OF ROWS returned by this query:

(SELECT Alias1 . *, 
        Alias2 . * 
 FROM   Table1 AS Alias1 
        LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
 WHERE  Alias1.Col1 != 'blah1' 
        AND Alias1.Col2 LIKE 'blah2' 
        AND ( Alias1.Col3 LIKE 'blah3' 
               OR Alias1.Col3 LIKE 'blah4' ) 
        AND Alias1.Col4 = 'blah5' 
        AND Alias2.Col7 LIKE 'blah6' 
 ORDER  BY Alias1.Col6 DESC 
 LIMIT  50) 

UNION 

(SELECT Alias1 . *, 
        Alias2 . * 
 FROM   Table1 AS Alias1 
        LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
 WHERE  Alias1.Col1 != 'blah1' 
        AND Alias1.Col2 LIKE 'blah2' 
        AND ( Alias1.Col3 LIKE 'blah3' 
               OR Alias1.Col3 LIKE 'blah4' ) 
        AND Alias1.Col4 = 'blah5' 
        AND Alias2.Col5 LIKE 'blah6' 
 ORDER  BY Alias1.Col6 DESC 
 LIMIT  50) 

UNION 

(SELECT Alias1 . *, 
        Alias2 . * 
 FROM   Table1 AS Alias1 
        LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
 WHERE  Alias1.Col1 != 'blah1' 
        AND Alias1.Col2 LIKE 'blah2'
        AND ( Alias1.Col3 LIKE 'blah3' 
               OR Alias1.Col3 LIKE 'blah4' ) 
        AND Alias1.Col4 = 'blah5' 
 ORDER  BY Alias1.Col6 DESC 
 LIMIT  50) 

I suppose I could just use PHP to execute the query and then do a mysql_num_rows on the results, but I want to do it directly with SQL because I heard that’s faster as it saves a step.

Thanks!

EDIT:

The following is giving me error (#1060 – Duplicate column name ‘Col1’):

   SELECT COUNT(*) FROM (
   (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2' 
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
            AND Alias2.Col7 LIKE 'blah6' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) 

    UNION 

    (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2' 
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
            AND Alias2.Col5 LIKE 'blah6' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) 

    UNION 

    (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2'
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50)
   ) a

The following is giving me error (#1064 – You have an error in your SQL syntax; ):

 SELECT COUNT(*) FROM (
   (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2' 
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
            AND Alias2.Col7 LIKE 'blah6' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) a

    UNION 

    (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2' 
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
            AND Alias2.Col5 LIKE 'blah6' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) b

    UNION 

    (SELECT Alias1 . *, 
            Alias2 . * 
     FROM   Table1 AS Alias1 
            LEFT JOIN Table2 AS Alias2 USING ( Col1 ) 
     WHERE  Alias1.Col1 != 'blah1' 
            AND Alias1.Col2 LIKE 'blah2'
            AND ( Alias1.Col3 LIKE 'blah3' 
                   OR Alias1.Col3 LIKE 'blah4' ) 
            AND Alias1.Col4 = 'blah5' 
     ORDER  BY Alias1.Col6 DESC 
     LIMIT  50) c
   ) z
  • 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-24T23:40:01+00:00Added an answer on May 24, 2026 at 11:40 pm

    You need to specify a unique alias for your Column Col1 since it is used in a join which requires unique columns to match against each other. It will throw an error 1060 if it comes across duplicated columns. Check this: Duplicate column names in SQL query

    In your case, the MySQL gets confused with Col1 in table 1 and table 2 and hence the error. Once you have used that, you can SELECT COUNT(*) from YourQuery

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This feels like a really basic question, but I haven't been able to find
I feel like I should know this, but I haven't been able to figure
I feel like the answer to this question is really simple, but I really
I feel like this is a dumb question and I'm missing something, but I
I'm sorry, but this is beginning to feel like kicking myself in the head.
I feel like this is a stupid question because it seems like common sense
I've seen a few examples of RSS Feeds in ASP.NET MVC, like this ,
This is a question I intend to answer myself, but please feel free to
Disclaimer: This is not actually a programming question, but I feel the audience on
First let me say that I really feel directionless on this question. I am

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.