gpt4 book ai didi

mysql - SQL/MySQL/PostGres - 当价格更新时返回所有列和记录

转载 作者:行者123 更新时间:2023-11-29 17:46:48 24 4
gpt4 key购买 nike

我有一个包含数千个拍卖元素条目的表。有些拍卖品会立即售出,而另一些则会在随后的拍卖中以较低的价格重新上市。每个拍卖元素条目在数据库中都有一个唯一的 ID(因此,当重新输入元素时,它会获得一个新的列表 ID),而且还具有一个不会随重新列表而更改的唯一元素标识符。

此外,还有拍卖品的类别,以及特定的品牌和类型(例如“家具”类别有很多列表,其中制造商“la-z-boy”是一个品牌,然后是“躺椅”)是一种更具体的类型)。

重新列出的商品将使用新的唯一 ID 和新价格重新输入数据库,但唯一商品标识符保持不变,所有其他列/属性保持不变。我可以找到所有价格发生变化的商品:

SELECT category, unique_item_identifier FROM auction_listings WHERE category='furniture' GROUP BY unique_item_identifier HAVING COUNT(*)>1; 

这个查询的问题是它只返回唯一标识符、类别和计数,但我无法查看价格变化。另外,如果我尝试更具体地查询查询,例如

SELECT category, brand, unique_item_identifier FROM auction_listings WHERE category='furniture' GROUP BY unique_item_identifier HAVING COUNT(*)>1;

此操作失败,因为:“SELECT 列表的表达式 #2 不在 GROUP BY 子句中,并且包含非聚合列,该列在功能上不依赖于 GROUP BY 子句中的列;这与 sql_mode=only_full_group_by 不兼容”

我想查看所有价格发生变化的 la-z-boys,然后看看这些价格变化是什么。我还希望能够运行一个查询,显示所有价格变化的 la-z-boys(无论类型),因此我还希望能够选择类别、品牌,输入 WHERE Brand='la- z-boy'(然后显示所有价格发生变化的 la-z-boys,输出包括不同的价格)。

我正在寻找诸如以下的输出:

unique-1 la-z-boy recliner price: $1000 entered on 1/1/2018
unique-1 la-z-boy recliner price: $800 entered on 2/1/2018
unique-2 la-z-boy recliner price: $1,200 entered on 1/1/2018
unique-2 la-z-boy recliner price: $1,050 entered on 2/1/2018
unique-2 la-z-boy recliner price: $950 entered on 3/1/2018
unique-3 la-z-boy couch price: $1,200 entered on 1/1/2018
unique-3 la-z-boy couch price: $1,000 entered on 2/1/2018

提前致谢 - 我在这里阅读了数十个与此特定请求非常接近的答案,但无法明确找到此答案,也无法从其他答案中找出如何做到这一点。

最佳答案

您尚未提供示例数据,但希望以下内容足以演示原理

+------+--------+----------+-------------+--------+
| id | userID | industry | companyName | salary |
+------+--------+----------+-------------+--------+
| 1 | 2 | 55 | abc company | 55 |
| 2 | 2 | 55 | xyz company | 75 |
| 3 | 2 | 56 | 123 company | 85 |
| 4 | 3 | 12 | cjf company | 25 |
| 5 | 4 | 52 | xxx company | 77 |
| 6 | 4 | 65 | yyy company | 99 |
+------+--------+----------+-------------+--------+
6 rows in set (0.00 sec)

如果在子查询中我们识别出薪资变化超过 1 次的人员,我们可以返回以获取详细信息

select userid,companyname,salary
from uex
where userid in(
select userid
from uex
group by userid having count(distinct salary) > 1
);
+--------+-------------+--------+
| userid | companyname | salary |
+--------+-------------+--------+
| 2 | abc company | 55 |
| 2 | xyz company | 75 |
| 2 | 123 company | 85 |
| 4 | xxx company | 77 |
| 4 | yyy company | 99 |
+--------+-------------+--------+
5 rows in set (0.02 sec)

如果要选择特定用户,请在子查询中包含 where 子句

select userid,companyname,salary
from uex
where userid in(
select userid
from uex
where userid = 2
group by userid having count(distinct salary) > 1
);

关于mysql - SQL/MySQL/PostGres - 当价格更新时返回所有列和记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49751575/

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