Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何按包含特定值的组选择分组?_Mysql - Fatal编程技术网

Mysql 如何按包含特定值的组选择分组?

Mysql 如何按包含特定值的组选择分组?,mysql,Mysql,我有一份订单表,其格式如下: ╔══════════╦══════════╦══════════╦═══════╦══════════════╦═══════════════╗ ║ Order ID ║ Subtotal ║ Shipping ║ Total ║ Product Name ║ Product Price ║ ╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣ ║ 1 ║

我有一份订单表,其格式如下:

╔══════════╦══════════╦══════════╦═══════╦══════════════╦═══════════════╗
║ Order ID ║ Subtotal ║ Shipping ║ Total ║ Product Name ║ Product Price ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 1        ║ 30       ║ 5        ║ 35    ║ Apple        ║ 10            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 1        ║          ║          ║       ║ Banana       ║ 10            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 1        ║          ║          ║       ║ Coffee       ║ 10            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 2        ║ 30       ║ 5        ║ 35    ║ Peach        ║ 20            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 2        ║          ║          ║       ║ Banana       ║ 10            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 3        ║ 20       ║ 3        ║ 23    ║ Peach        ║ 20            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 4        ║ 40       ║ 10       ║ 50    ║ Apple        ║ 10            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 4        ║          ║          ║       ║ Coffee       ║ 10            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 4        ║          ║          ║       ║ Peach        ║ 20            ║
╚══════════╩══════════╩══════════╩═══════╩══════════════╩═══════════════╝
所有订单编号均为总计-小计和发货,以下各行为空

我正在调查某一特定产品的销售情况

例如,我希望有一个查询,选择包含“香蕉”的所有订单,并返回订单的小计、发货和总计以及产品价格(有时不在同一行):

也就是说,它会返回这个:

╔══════════╦══════════╦══════════╦═══════╦══════════════╦═══════════════╗
║ Order ID ║ Subtotal ║ Shipping ║ Total ║ Product Name ║ Product Price ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 1        ║ 30       ║ 5        ║ 35    ║ Banana       ║ 10            ║
╠══════════╬══════════╬══════════╬═══════╬══════════════╬═══════════════╣
║ 2        ║ 30       ║ 5        ║ 35    ║ Banana       ║ 10            ║
╚══════════╩══════════╩══════════╩═══════╩══════════════╩═══════════════╝

您可以将表连接到自身:

select 
  b.`Order ID`,
  max(s.Subtotal) as 'Subtotal',
  max(s.Shipping) as 'Shipping',
  max(s.Total) as 'Total',
  b.`Product Name`,
  b.`Product Price`
from orders b
  join orders s on s.`Order ID`=b.`Order ID`
where b.`Product Name`='Banana'
group by 
  b.`Order ID`, 
  b.`Product Name`,
  b.`Product Price`

参见a。

是的,显然不理想,但遗憾的是,这是我手头的平台导出。这是我必须处理的问题,但感谢您的反馈。这是一个奇怪的顺序,您不知道命名项目的数量。这非常有效,但您能帮助我了解它是如何工作的吗?所以我们加入两个表,但只加入那些产品名称符合我们标准的字段?首先,您要找出哪些订单中有香蕉。然后使用相同的orders表连接订单,从Total行中获取小计+发货+总值。