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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:57:26+00:00 2026-05-13T19:57:26+00:00

(PHP/MySQL) Let me preface by saying that this works, and that I’m wondering if

  • 0

(PHP/MySQL) Let me preface by saying that this works, and that I’m wondering if there’s a cleaner way. This seems a bit… brutish? And I’m not known for my efficient coding. :-/

TABLE (many-to-many)
scenid   mapid
  AA      01
  AA      02
  AA      04
  BB      01
  BB      04
  CC      02
  CC      03
  CC      05
  DD      01
  DD      02
  DD      03

You are searching for scenarios that have ALL of these maps: 01, 02, 03
(having all + some others is ok)

My current method is to use this query:

SELECT scenid, COUNT(scenid) FROM `TABLE`
WHERE mapid IN ( 01, 02, 03 )
GROUP BY scenid ORDER BY COUNT(scenid) DESC, scenid ASC

Which would produce this list:

scenid  COUNT
  DD      3
  AA      2
  CC      2
  BB      1

Then, within PHP, I run a while loop that copies scenids to a new array so long as the COUNT is still == the total number of mapids (in this case, 3). This trims off all results that contain only some of the required maps.

Again, this works. But I’m still very new to MySQL & PHP (and they’re my first steps into programming) so I’m always wondering if my “works” is another person’s “did you see that idiot just reinvent the wheel? lulz“

Is there a better way? Thank you for your time.

  • 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-13T19:57:27+00:00Added an answer on May 13, 2026 at 7:57 pm

    Your query looks ok, but you could do the filtering in SQL using HAVING clause:

    SELECT scenid, COUNT(*)
    FROM `TABLE`
    WHERE mapid IN ( 01, 02, 03 )
    GROUP BY scenid
    HAVING COUNT(*) = 3
    ORDER BY scenid ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me preface my question by saying that I am very new to mysql
Let's say that I have an error in a php/mysql query : $query =
I am having a difficult time sorting through this PHP/MySQL issue. Let me show
I've an issue, which is about php syntax/mysql in drupal: Let's say that userA
I'm having issues escaping/stripping strings with PHP/MySQL - there always seems to be redundant
what is PHP's safest encrypt/decrypt method, in use with MySQL - to store let's
Are PHP/mysql prepared statements possible when mysqli and PDO are not available? Are there
a bit of a PHP / MySQL newbie here... I've been building a PHP-based
We have a custom PHP/MySQL web application that gets updated and copied (using SFTP)
I have a PHP/MySQL sign-up system that limits the number of people who can

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.