我只是在mysql中加入了连接和诸如此类的东西。我有一个小问题我正在努力,我想根据他们所属的促销活动返回产品和相应客户/公司的最低价格。我能够获得产品,公司和价格,但我无法获得最低价格。
当前输出如下:
select G.promotionid, name, startdate, enddate, issuer, company_name, productid,
promotionprice from promotion_general G inner join company_promotions C on G.promotionid =
C.promotionid inner join productpromotions P on G.promotionid = P.promotionid order by
company_name, productid;
+-------------+------------+------------+------------+----------------+------------------------+-----------+----------------+
| promotionid | name | startdate | enddate | issuer | company_name | productid | promotionprice |
+-------------+------------+------------+------------+----------------+------------------------+-----------+----------------+
| 3 | promo2 | 2014-05-08 | 2014-06-08 | zakiir | Company C | DEMOB | 300.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company F | DEMOA | 500.00 |
| 3 | promo2 | 2014-05-08 | 2014-06-08 | zakiir | Company F | DEMOB | 300.00 |
| 2 | promo2 | 2014-08-08 | 2014-09-08 | zakiir | Company F | DEMOB | 650.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company F | DEMOB | 600.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company F | DEMOC | 650.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company G | DEMOA | 500.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company G | DEMOB | 600.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company G | DEMOC | 650.00 |
+-------------+------------+------------+------------+----------------+------------------------+-----------+----------------+
组件表如下所示:
select * from productpromotions;
+-------------+-----------+----------------+---------------------+
| promotionid | productid | promotionprice | productdateset |
+-------------+-----------+----------------+---------------------+
| 1 | DEMOA | 500.00 | 2014-08-07 10:54:07 |
| 1 | DEMOB | 600.00 | 2014-08-07 00:02:03 |
| 2 | DEMOB | 650.00 | 2014-08-07 00:02:03 |
| 1 | DEMOC | 650.00 | 2014-08-07 00:02:07 |
| 3 | DEMOB | 300.00 | 2014-08-07 00:02:03 |
+-------------+-----------+----------------+---------------------+
和
select * from company_products;
ERROR 1146 (42S02): Table 'afl_discounts.company_products' doesn't exist
mysql> select * from company_promotions;
+-------------+------------------------+
| promotionid | company_name |
+-------------+------------------------+
| 1 | Company G |
| 2 | Company F |
| 3 | Company C |
| 1 | Company F |
| 3 | Company F |
+-------------+------------------------+
和
select * from promotion_general;
+-------------+------------+------------+------------+----------------+
| promotionid | name | startdate | enddate | issuer |
+-------------+------------+------------+------------+----------------+
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost |
| 2 | promo2 | 2014-08-08 | 2014-09-08 | zakiir |
| 3 | promo2 | 2014-05-08 | 2014-06-08 | zakiir |
| 4 | promo3 | 2014-05-08 | 2014-06-08 | zakiir |
+-------------+------------+------------+------------+----------------+
我需要做些什么来使我的输出看起来像这样?
+-------------+------------+------------+------------+----------------+------------------------+-----------+----------------+
| promotionid | name | startdate | enddate | issuer | company_name | productid | promotionprice |
+-------------+------------+------------+------------+----------------+------------------------+-----------+----------------+
| 3 | promo2 | 2014-05-08 | 2014-06-08 | zakiir | Company C | DEMOB | 300.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company F | DEMOA | 500.00 |
| 3 | promo2 | 2014-05-08 | 2014-06-08 | zakiir | Company F | DEMOB | 300.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company F | DEMOC | 650.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company G | DEMOA | 500.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company G | DEMOB | 600.00 |
| 1 | test promo | 2014-07-08 | 2014-08-08 | root@localhost | Company G | DEMOC | 650.00 |
+-------------+------------+------------+------------+----------------+------------------------+-----------+----------------+
如果已删除DEMO B的重复且更昂贵的价格。
我知道它很长,但我很难过。
谢谢,
答案 0 :(得分:1)
尝试运行此..我认为它会做你想要的。
SELECT
*
FROM
( SELECT
cp.promotionid,
name,
startdate,
enddate,
issuer,
company_name,
p.productid,
p.promotionprice
FROM company_promotions cp
JOIN productpromotions p ON p.promotionid = cp.promotionid
JOIN promotion_general pg ON pg.promotionid = cp.promotionid
ORDER BY cp.promotionid DESC
) t
GROUP BY company_name, productid