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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:47:12+00:00 2026-05-26T12:47:12+00:00

I’ve always been confused on how to combine multiple MySQL queries into one. I’ve

  • 0

I’ve always been confused on how to combine multiple MySQL queries into one. I’ve been pulling my hair out trying to do the following. First, I do a query to get a result of each vendor and how many products they have:

SELECT vendor_name, COUNT(vendor_name) AS cnt FROM products GROUP BY vendor_name

Next, as I only want those vendors who have 10 or more products, I do the following PHP IF statement. I don’t know how to incorporate this in the above SELECT, so I do it via PHP. I know, it’s not the best, but when you don’t know the best way, you need to do it some way:

if ($database_row['cnt']>=10) {

Within this IF statement, I then do a query on each of the vendors that have 10 or more products:

SELECT id FROM products WHERE vendor_name="'.$database_row['vendor_name'].'"

Which gives me the id’s of each product for vendors that have at least 10 products. I know, it’s sloppy, but it generates the correct results. The issue is that I need all of this to be in one query and for the life of me I can’t figure it out. Both of these queries are in the same database, and it doesn’t seem to be that complex of a request, which is really just “show me the products of those vendors who have 10 or more products”.

Can the above be combined into one query? The current full PHP/MySQL code is as follows:

$database_result = @mysql_query('SELECT vendor_name, COUNT(vendor_name) AS cnt FROM products GROUP BY vendor_name',$database_conn);
while($database_row = @mysql_fetch_array($database_result)) {
  if ($database_row['cnt']>=10) {
    $database_result2 = @mysql_query('SELECT id FROM products WHERE vendor_name="'.$database_row['vendor_name'].'"',$database_conn);
    // Process results
  }
}

Thanks in advance!


Here’s the full single query that now works exactly as I wanted. Maybe there’s a clever way optimize the query, but it works and seems fast enough. Thanks to all!

SELECT
  p3.*
FROM
(
  SELECT
    p1.id,
    p1.department,
    p1.end_date,
    p1.quantity_sold,
    p2.vendor_name
  FROM
    products p1
  INNER JOIN
  (
    SELECT
      vendor_name
    FROM
      products
    GROUP BY
      vendor_name
    HAVING
      COUNT(*) >= 10
  ) p2
  ON
    p1.vendor_name = p2.vendor_name
  AND
    p1.sold_out = 0
  AND
    p1.end_date > NOW()
  ORDER BY
    p1.end_date DESC
) AS p3
GROUP BY
  p3.vendor_name
ORDER BY
  p3.quantity_sold DESC
  • 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-26T12:47:13+00:00Added an answer on May 26, 2026 at 12:47 pm

    Try this

    SELECT 
        p1.id,
        p1.vendor_name 
    FROM 
        products p1 
        INNER JOIN
        (
           SELECT 
              vendor_name,
              COUNT(vendor_name) as productCount 
           FROM 
              products 
           GROUP BY 
              vendor_name
           HAVING
              productCount >= 10
        ) p2
        ON p1.vendor_name=p2.vendor_name
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from

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.