gpt4 book ai didi

mysql - 在 MySQL 中实现复杂查询

转载 作者:行者123 更新时间:2023-11-29 07:25:59 25 4
gpt4 key购买 nike

我有 3 张 table :

  1. 桌面产品
  2. 表格子产品
  3. 库存。

我想像下面的结果表一样连接这些表。事实上,我想要所有产品具有最低价格计数大于零并作为最新记录插入!

如何进行查询?

schema of my tables

更多说明:

table product
----------------------------------
pid | title | desc | content |
----------------------------------
1 | lumia 920| ..... | ...... |
----------------------------------
2 | galaxys6 | .... | ...... |
----------------------------------

table sub_product
------------------------
subid |pid| name |
------------------------
1 | 1 | yellow |
------------------------
2 | 1 | black |
------------------------
3 | 2 | 32 GB |


table stock
-----------------------------------------------
sid |subid| price | count | inserted_date |
-----------------------------------------------
1 | 1 | 100 | 5 | 2015-01-01 |
-----------------------------------------------
2 | 1 | 150 | 9 | 2015-01-02 |
-----------------------------------------------
3 | 1 | 100 | 0 | 2015-02-02 |
-----------------------------------------------
4 | 2 | 111 | 1 | 2015-02-21 |
-----------------------------------------------
5 | 3 | 50 | 7 | 2015-02-01 |
-----------------------------------------------
6 | 3 | 10 | 4 | 2015-03-06 |
-----------------------------------------------
7 | 3 | 400 | 9 | 2015-06-06 |
-----------------------------------------------


table result
------------------------------------------------------------
pid |subid| title | name | price | count | inserted_date
------------------------------------------------------------
1 | 2 |lumia 920| black | 111 | 1 | 2015-02-21
------------------------------------------------------------
2 | 3 |galaxy s6| 32 GB | 10 | 4 | 2015-03-06
------------------------------------------------------------

正如您在产品表中看到的,我们有两款产品:lumia 920 和 Galaxy s6在 sub_product 中,我们有 3 个与产品相关的项目。还有库存,我保存了每件商品的价格和数量的所有修改所以我想返回每个子产品的最新修改作为其结果价格最低的子产品的当前状态但如果计数为零,则应返回具有上述条件的另一个 sub_product。

最佳答案

    Select * From stock s Join sub_product sp On s.sub_productid =     sp.sub_productid Join product p On p.productid = sp.productid
Where s.counte > 0 And s.date_insert in (Select MAX(date_insert) as d From stock ss
where s.sub_productid = ss.sub_productid group by sub_productid)

关于mysql - 在 MySQL 中实现复杂查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34551580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com