Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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_Sql_Laravel - Fatal编程技术网

Mysql 查找联接两个表和分组方式接收的项目数量之和

Mysql 查找联接两个表和分组方式接收的项目数量之和,mysql,sql,laravel,Mysql,Sql,Laravel,我想找出联接两个表收到的项目数量之和 我试过了 select * from items join receive_items ri on items.id = ri.item_id group by ri.item_id +----------------+----------+--------------+ |id | Name | quantity | categories | +----------------+----------+--------------+

我想找出联接两个表收到的项目数量之和

我试过了

select * from items join receive_items ri on items.id = ri.item_id group by ri.item_id


+----------------+----------+--------------+
|id |   Name     | quantity |  categories  | 
+----------------+----------+--------------+
| 1 | Soap       |   5      |  cleanness   |
| 2 | Tea Bags   |   5      |  sundries    |
| 3 | Powder Milk|   5      |  sundries    |
+----------------+----------+--------------+

从上表中可以看出,它只返回接收项目的数量值,而不是每个项目的数量总和,而每个项目有多个

我建议您阅读有关查询构建的更多信息

在您的情况下,最简单的查询是:
选择items.id作为id,items.name作为名称,sum(receive_items.quantity)作为数量,items.categories作为项目左侧的类别加入receive_items on items.id=receive_items.item_id按items.id分组

这是一个简单的
加入
分组方式
。您尝试过什么?我尝试过从项目中选择*加入接收项目项目ri on items.id=ri.item\u id group by ri.item\u id比您更有效,我将阅读有关查询构建的更多信息。