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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:16:43+00:00 2026-06-03T03:16:43+00:00

I have a feeling CASE should be used here but I am not sure.

  • 0

I have a feeling CASE should be used here but I am not sure. I’m trying to take the sex (m/f) from the general table and depending on the sex, get the build from the corresponding table. For example, when the sex is m, the query should know that by going to the males table and selecting the male_build. I put together an output along with a sample query to give an idea of what I’m trying to accomplish:

members table
member_id | member_name
------------------------
     1         Herb
     2         Karen
     3         Megan
     4         Pablo

males table
male_id | member_id | male_build
---------------------------------
    1         1        muscular
    2         4          fat

females table
female_id | member_id | female_build
-------------------------------------
    1           2           thin
    2           3           fat 

general table
general_id | member_id | sex
-----------------------------
     1          1         m
     2          2         f
     3          3         f
     4          4         m

The output should be like this:

1. Herb is a muscular male.
2. Karen is a thin female.
3. Megan is a fat female.
4. Pablo is a fat male.
   -> query = "SELECT g.general_id, g.member_id, g.sex,   
              (CASE when g.sex=m THEN SELECT ma.male_build AS build 
                   FROM males ma WHERE ma.member_id=g.member_id,
              CASE when g.sex=f THEN SELECT fe.female_build AS build 
                   FROM females fe WHERE fe.member_id=g.member_id),
               m.member_name 
               FROM members m
               JOIN members m ON g.member_id=m.member_id 
               WHERE g.sex='m' OR g.sex='f'";

Looking for an efficient query for this task.

  • 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-03T03:16:44+00:00Added an answer on June 3, 2026 at 3:16 am

    When you are having to do queries like this, it is a good idea to rethink your database design schemas. That being said, this should work:

    SELECT mem.*, g.*, coalesce(m.male_build, f.female_build) as build
    from members_table mem
    inner join general g on mem.meber_id = g.member_id
    left join males m on mem.member_id = m.member_id
    left join females f on mem.member_id = f.member_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a general gut feeling about the difference but I'm unable to pinpoint
I have a feeling that I already know the answer to this one, but
I have a feeling that there must be client-server synchronization patterns out there. But
I have a feeling this XML is not valid, can someone please explain why?
I have the feeling the answer to this question is no but I figured
Similar: How can I delete duplicate rows in a table I have a feeling
I have a very simple case that I think would benefit from using templates
I have a static method that should take two objects and a float as
Folks, I have a feeling there's a classic design pattern that covers this case,
I have a feeling the answer is no but thought I'd ask just in

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.