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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:28:26+00:00 2026-05-15T16:28:26+00:00

I have three tables, ‘A’, ‘B’ and ‘C’. I have query on ‘A’ and

  • 0

I have three tables, ‘A’, ‘B’ and ‘C’. I have query on ‘A’ and ‘B’, but I want to add a field that tells me whether or not there is one or more (I dont’ care how many) ‘C’ that are foreign keyed to ‘A’.

Here’s what I have:

SELECT    A.A_id, A.col_2, col_3, B.col_2, A.col_4
          count(C.id) as C_count
FROM      A
JOIN      B ON (A.B_id = B.B_id)
LEFT JOIN C ON (A.A_id = C.A_id)
WHERE     A.A_id = ?
GROUP BY  A.A_id, A.col_2, col_3, B.col_2, A.col_4
ORDER BY  CASE WHEN A.col_2 = ?
               THEN 0
               ELSE 1 
          END, col_3;

It seems a little inefficient, both because I have to list all those fields in the GROUP BY and also because I’m counting where all I really want is whether there is at least one match or not. Can this be improved?

  • 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-15T16:28:27+00:00Added an answer on May 15, 2026 at 4:28 pm

    use Exists with a subquery instead…

     Select A.A_id, A.col_2, col_3, 
        B.col_2, A.col_4, 
        Case When Exists (Select * From C
                          Where A_id = A.A_id)
             Then 1 Else 0 End As C_Exists
     From A Join B 
         On (A.B_id = B.B_id) 
     Where A.A_id = ?    
     Order By Case When A.col_2 = ? 
               Then 0 Else 1 End, col_3;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables in an SQL 2005 database, that I need to query
I have three tables in MySQL that are related, yet not technically linked to
I have three tables which I want to query in MySQ. As follows: **Table:
I have three tables (individuals, groups & recordlabels) and want use the information between
I have three tables, one fact and two dimensions. I want to make a
I have three tables that look like these: PROD Prod_ID|Desc ------------ P1|Foo1 P2|Foo2 P3|Foo3
I have three tables, places , place_user , users . I want to list
I have three tables: USER: user_id (pk); username FIELD: field_id (pk); name METADATA: metadata_id
i have three tables in my database book,author,publishing ... i want the user can
I have three tables like this CREATE TABLE `money`.`categories` ( `ID` bigint(20) unsigned NOT

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.