Mysql内部连接并具有计数
嘿,这里有一个问题: 我有3个表,我们称它们为items、article和order\u point,它们的简化形式如下:Mysql内部连接并具有计数,mysql,Mysql,嘿,这里有一个问题: 我有3个表,我们称它们为items、article和order\u point,它们的简化形式如下: item = |item_id|article_id|, article = |article_id|article_info|, order_point=|op_id|article_id|op_amount| items表包含存储中的物理项,每个物理项一行。article表包含关于每种类型的物理项目的信息,order_表示每笔物品在任何时候都应保存的数量名称
item = |item_id|article_id|,
article = |article_id|article_info|,
order_point=|op_id|article_id|op_amount|
items表包含存储中的物理项,每个物理项一行。article表包含关于每种类型的物理项目的信息,order_表示每笔物品在任何时候都应保存的数量名称
现在,我想做的是获取每个商品id,它在商店中的商品数量少于订单数量
所以我想要的是这样的:
SELECT article_id
FROM item INNER JOIN order_point
ON (item.article_id = order_point.article_id GROUP BY item.article_id
HAVING COUNT(item)<order_point.op_amount)
SELECT *
FROM order_point
INNER JOIN (SELECT item.article_id, COUNT(*) AS in_store
FROM item INNER JOIN order_point
ON item.article_id = order_point.article_id
GROUP BY item.article_id) AS TEMP
ON order_point.article_id = TEMP.article_id
WHERE TEMP.in_store<order_point.amount
GROUP BY order_point.article_id
但是上面的代码是无效的,这是工作日的最后几分钟,所以让我们看看是否有人在我明天之前解决它
编辑:查询中的问题类似于having子句中的order\u point.op\u amount未知。假设您连接的列没有重复项,这是一种非常常见的情况: A和B的内部连接给出相交B的结果,即维恩图相交的内部部分 A和B的外部连接给出了并集B的结果,即维恩图并集的外部部分 例子 假设您有两个表,每个表只有一列,数据如下:
A B
- -
1 3
2 4
3 5
4 6
请注意,1,2是A独有的,3,4是常见的,5,6是B独有的
内连接
使用任何一个等价查询的内部联接都会给出两个表的交集,即它们共有的两行
select * from a INNER JOIN b on a.a = b.b;
select a.*,b.* from a,b where a.a = b.b;
a | b
--+--
3 | 3
4 | 4
左外连接
左外部联接将给出A中的所有行,以及B中的任何公共行
select * from a LEFT OUTER JOIN b on a.a = b.b;
select a.*,b.* from a,b where a.a = b.b(+);
a | b
--+-----
1 | null
2 | null
3 | 3
4 | 4
完全外接
一个完整的外部联接将提供A和B的并集,即A中的所有行和B中的所有行。如果A中的某些内容在B中没有相应的基准,则B部分为空,反之亦然
select * from a FULL OUTER JOIN b on a.a = b.b;
a | b
-----+-----
1 | null
2 | null
3 | 3
4 | 4
null | 6
null | 5
我希望这能解决你的问题!祝你今天愉快 我是这样解决的:
SELECT article_id
FROM item INNER JOIN order_point
ON (item.article_id = order_point.article_id GROUP BY item.article_id
HAVING COUNT(item)<order_point.op_amount)
SELECT *
FROM order_point
INNER JOIN (SELECT item.article_id, COUNT(*) AS in_store
FROM item INNER JOIN order_point
ON item.article_id = order_point.article_id
GROUP BY item.article_id) AS TEMP
ON order_point.article_id = TEMP.article_id
WHERE TEMP.in_store<order_point.amount
GROUP BY order_point.article_id
这给了我存储的项目少于请求数量的行,这就是我所追求的。您没有按组分组与按组相同的问题,据我所知,您不需要在MYSQL中进行分组。无论如何,我会包括它。你应该问问题使用适当的输入和预期的输出。谢谢你的建议,我不知道该网站,并将在未来做。