0

采购表:

========================
id|username|product|price|
01|Desmond |tv     |$40  |
02|Desmond |watch  |$20  |
03|Desmond |ring   |$30  |
04|Desmond |laptop |$40  |
========================

产品表:

============================
id|product|type |promotion_ID|
01|tv     |big  |001         |
02|watch  |small|002         |
03|ring   |small|002         |
04|laptop |big  |001         |
============================

促销编号:

==============================
id|promotion_ID|promotion_name|
01|001         |mid-year      |
02|002         |end-year      |
==============================

我想基于 while 循环方法显示采购表的结果,以显示来自 mysql 的所有信息,这没问题,但我想根据采购表的促销 ID/促销名称对它们进行分组。我可以知道,我该怎么做?

例如显示结果:

年中

=======================
username|product|price|
Desmond |tv     |$40  |
Desmond |laptop |$40  |
=======================

年终

=======================
username|product|price|
Desmond |watch  |$20  |
Desmond |ring   |$30  |
=======================
4

3 回答 3

0

您是否只是在寻找ORDER BYJOIN

SELECT promotion_name, 
    username, 
    p.product,
    price
FROM Purchase P
    JOIN Product PR ON P.Product=PR.Product
    JOIN Promotion PO ON PR.Promotion_Id=PO.Promotion_Id
ORDER BY promotion_name, p.product

关于您的数据库,我不建议将 Product 存储在 Purchase 表中——您应该存储 Product_Id 。

于 2013-03-22T04:32:02.887 回答
0
This may help you..

Select username,product,price from Purchase
Join Product on Product.product = Purchase.product 
Join Promotion On promotion.promotion_ID  = Product.promotion_ID
where promotion_name = 'mid-year' // pass your argument
Order by Product.product
于 2013-03-22T04:35:45.800 回答
0

尝试

SELECT pur.username, pur.product, pur.price
FROM tbl_purchase pur 
INNER JOIN tbl_product pro USING(product)
INNER JOIN tbl_promotion prm USING(promotion_ID)
ORDER BY prm.promotion_name

注意:您没有引用所有表的主键,所以在列productpromotion_id所有表上建立索引

于 2013-03-22T04:38:12.137 回答