English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have two tables one for products and one for customers and the relation between them is many to many I want to select the product name that all the customer have bought
how to do that query ???

2006-12-22 04:43:54 · 6 answers · asked by I.M. 2 in Computers & Internet Programming & Design

the tables are

__________
Customers
__________

cusID
cusName


__________
Products
__________
ProID
ProName
cusID


two records for the customer

________________________
cusID | cusName |
________________________
1 | name1 |
2 | name2 |
________________________

records for the Products

_________________________________
ProID | ProName | cusID |
_________________________________
1 | pro1 | 1 |
2 | pro1 | 2 |
3 | pro2 | 1 |
_________________________________

I want the query that selects the product name that all the customers have bought so it will be pro1

2006-12-22 06:15:48 · update #1

6 answers

Ok assuming this table structure:

Products
========
ProductID
ProductName
ProductDescription

Customers
==========
CustomerID
CustomerName


What exactly is the relationship which the two tables have in common?

Do you have like an Invoice table or something where the Customer and Product are linked?

2006-12-22 04:50:03 · answer #1 · answered by Anonymous · 0 0

Select * from customers a left outer join products b where a.cusid = b.cusid

Note that if the unique index on products is the productID, every product will be associated with no more than one customer. If this is the case, the value in products.cusid likely reflects the last customer who purchased the product - not all customers that have purchased the product.

2006-12-22 06:33:13 · answer #2 · answered by Anonymous · 0 0

ZOplex is right. You have to specify the relationship between two tables and pl brief the columns detail and if you have any transaction table.

2006-12-22 05:00:28 · answer #3 · answered by V 5 · 0 0

NO manches!!!!!!!! If you give me an example of 2 records for each table I can get the sql query for you, now
manuel220@yahoo.com

2006-12-22 05:01:40 · answer #4 · answered by manuel220 3 · 0 0

You need to add ORDER BY TIMESTAMP DESCENDING. This will bring in the products most recent first. There are loads of sites that detail how to do this.

2016-05-23 15:58:48 · answer #5 · answered by Anonymous · 0 0

You're lucky man cos this is my job ....and i don't tokng about my self but i will answer you about what you really want ...
Table 1
--------------------
Products
idproduct
iduser
namep
type
cost
etc..
--------------------
Table2
-------------------
Customersinfo
iduser
name
age
etc
-------------------

$sql="SELECT * FROM `Products`,`Customersinfo` WHERE Products.iduser = customersinfo.iduser ";
---------------------------------

This all what you Need to do And Give me My Point .

2006-12-22 05:07:03 · answer #6 · answered by Rasmah.com 5 · 0 0

fedest.com, questions and answers