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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:56:34+00:00 2026-05-25T21:56:34+00:00

I have a table like this and type is innodb and id and sid

  • 0

I have a table like this and type is innodb and id and sid are indexed.

  id | sid
  ##########
   1 | 100
   1 | 101
   1 | 102
   1 | 103
   1 | 104
   1 | 105
   2 | 100
   2 | 101
   3 | 100
   3 | 101

I need id’s have sid 100,…105 like id=1.
I have this query

select t1.id
  from test as t1, test as t2, test as t3,
       test as t4, test as t5, test as t6
 where t1.sid=100
   and t2.sid=101
   and t3.sid=102
   and t4.sid=103
   and t5.sid=104
   and t6.sid=105
   and t1.id = t2.id = t3.id = t4.id = t5.id = t6.id

but phpmyadmin hangs when I run query.
The number of records is 2000.

What is optimized 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-05-25T21:56:35+00:00Added an answer on May 25, 2026 at 9:56 pm

    It is surprising that the SQL statement shown compiles/runs.

    However, you can probably rescue it by rewriting what is currently the last line to:

    select t1.id
      from test as t1, test as t2, test as t3,
           test as t4, test as t5, test as t6
     where t1.sid = 100
       and t2.sid = 101
       and t3.sid = 102
       and t4.sid = 103
       and t5.sid = 104
       and t6.sid = 105
       and t1.id = t2.id
       and t1.id = t3.id
       and t1.id = t4.id
       and t1.id = t5.id
       and t1.id = t6.id;
    

    Is this query optimal?

    Given your comment to Maulik Vora’s answer that the range { 100 .. 105 } is just an example, then we need to know whether it is always 6 values or whether it could be 5 or 7 or some other number. However, it is likely that the optimal structure uses a temporary table (call it ListToMatch with a non-null unique column SID) containing whatever values are needed, and that the query is:

    SELECT T.ID
      FROM Test AS T JOIN ListToMatch AS L ON T.SID = L.SID
     GROUP BY T.ID
     HAVING COUNT(*) = (SELECT COUNT(*) FROM ListToMatch);
    

    You insert the set of values you’re interested in to ListToMatch; you can then get the list of ID values that match. This is likely to be more nearly optimal, not least because it adapts to 1 value and 100 values as easily as it does to 6, whereas rewriting the SQL with a 100-way self-join doesn’t bear much thinking about.

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

Sidebar

Related Questions

i have a table like this: Table(MissioneID, Type) Type can be 1,2 or 3
I have a table like this in my database (SQL Server 2008) ID Type
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have this mysql table called comments which looks like this: commentID parentID type
I have a table like this ID | Type | Val0 | Val1 1
I have a table like this: +-------+---------+------+-----+---------+-------+ | Field | Type | Null |
I have first table like this: ID Email 100 a@a.com 200 b@b.com I have
I have a table like this: Table A Customer InvoiceNo Region Type Amount A001
I have a table like this: id | timestamp | type ----------------------- 1 2010-11-20
I have a table like this mysql> desc user_changes; +----------+--------------+------+-----+---------+-------+ | Field | Type

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.