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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:59:01+00:00 2026-06-16T05:59:01+00:00

I have this table with data: NAME | GROUP ———— a1 | A a22

  • 0

I have this table with data:

NAME | GROUP
------------
a1   | A
a22  | A
c1   | C
c2   | C
s1   | NULL
t1   | NULL 

How to select record named a1 and if this record has a group <> NULL include also other items from the same group?

I’ve tried subquery:

SELECT `name`, `group` FROM `tbl`
WHERE (`name` = 'a1' AND `group` IS NULL) OR `group` = (SELECT `group` FROM `tbl` WHERE `name` = 'a1')

EXPLAIN:

id  select_type  table  type  possible_keys  key      key_len  ref    rows  
 1  PRIMARY      tbl    ALL   PRIMARY        NULL     NULL     NULL   6 Using where
 2  SUBQUERY     tbl    const PRIMARY        PRIMARY  386      const  1

This works but I’m not sure if it’s 100% correct and efficient. Looking for a better solution, maybe with JOIN?

MySQL internals question: Is inner SELECT query executed for every searched record in the outer table? Is result of this query cached (like SELECT SQL_CACHE query)?

  • 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-16T05:59:02+00:00Added an answer on June 16, 2026 at 5:59 am

    The following query will return a resultset that matches the specification.

    SELECT DISTINCT COALESCE(t.name,s.name) AS `name`
      FROM `tbl` s
      LEFT
      JOIN `tbl` t
        ON t.group = s.group
     WHERE s.name = 'a1'
    

    If a row with name = 'a1' exists in the table, that row will be returned. The query will also return values of name that have a group value that matches any group value for a row with name = 'a1'.

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

Sidebar

Related Questions

I have a table and data like this: ID | Name | Group 1
I have this data in a table, for instance, id name parent parent_id 1
I have this table in my DB: Group - ID-Name - 1 -abc -
Say I have this table schema. ID AccNo Amount Say I have this data
I have this JSF table which is used to display data. I want to
I have no control over how the data is saved in this table. However,
My table have data structure like this cate_id task_id date_start date_end other 34 14
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have a data.table object similar to this one library(data.table) c <- data.table(CO =
I have a table like this: Application,Program,UsedObject It can have data like this: A,P1,ZZ

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.