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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:45+00:00 2026-05-26T08:25:45+00:00

So I have these 2 tables and I’m trying to write the most efficient

  • 0

So I have these 2 tables and I’m trying to write the most efficient query to output the distinct coupons for each manufacturer:

Manu Table
manu_id    manu_name
   1        Fujitsu 
   2           HP
   3        Brother

Products Table
prod_id    manu_id    prod_type   prod_coupon_img
    1         2          A4            1.jpg
    2         2         paper          1.jpg
    3         2         boxes          2.jpg
    4         3          ink           3.jpg

This query:

$sql = mysql_query("SELECT m.manu_id, p.prod_id, p.prod_type, p.prod_coupon_img
                    FROM manu m INNER JOIN products p 
                    WHERE m.manu_id=2 AND m.manu_id=p.manu_id");

Outputs:

prod_id    manu_id    prod_type  prod_coupon_img
    1         2          A4           1.jpg
    2         2         paper         1.jpg
    3         2         boxes         2.jpg

and this query:

$sqlDistinctCoupons = mysql_query("SELECT DISTINCT m.manu_id, p.prod_coupon_img 
                                   FROM manu m INNER JOIN products p 
                                   WHERE m.manu_id=2 AND m.manu_id=p.manu_id");

Outputs:

manu_id    prod_coupon_img
   2            1.jpg
   2            2.jpg

What I’m trying to accomplish now is to show the unique coupons HP is offering, so one for (A4 & paper), and one for boxes.
Here is my code to output that:

$DistinctCoupons .=   'Showing coupon for $row["prod_type"] products <img id="$row["prod_id"]" src="$row["prod_coupon_img"]" />';

How can I do this, how can I mix both queries to just one and output the distinct coupons? I tried 2 while loops but that didn’t work, and I’ve been reading that you shouldn’t use 2 while loops on db calls anyways, as it puts a lot of unneeded strain. Any ideas?

  • 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-26T08:25:45+00:00Added an answer on May 26, 2026 at 8:25 am
    $manu_name = mysql_real_escape_string($_POST['manu_name']);
    $query = "SELECT p2.prod_type, p2.prod_id, p2.prod_coupon_img
    FROM products p2 
    WHERE p2.id IN (
      SELECT Min(p.id)
      FROM products p 
      INNER JOIN manu m ON (m.manu_id = p.manu_id)
      WHERE m.manu_name = '$manu_name'
      GROUP BY prod_coupon_img) ";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have these tables: people(id, name), cars(person_id, car) and this query: SELECT c.car
i have these two tables , s(id,firstname,lastname,mydate,mount,sh) users(uid,email,password,nameoffice) and this query $sql=SELECT s.id,s.firstname,s.lastname,s.mydate,s.mount,users.nameOffice FROM
I have these tables: customer -------- customer_id int name varchar(255) order ----- order_id int
I have these tables: Projects(projectID, CreatedByID) Employees(empID,depID) Departments(depID,OfficeID) Offices(officeID) CreatedByID is a foreign key
Suppose you have these tables: Table Name: Salesman Fields: S_ID(Primary Key), Name Table Name:
I have these 3 tables + data: items : itemId, itemName data: 1, my
If I have these two tables: <table> <tr> <td>Small Length Content</td> </tr> </table> <table>
I have some tables. These tables all have one column in common called 'classified_id':
Does anyone have any suggestions for creating meta tables in a database? These tables
I have three tables in my DB (actually a few more than that) these

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.