MySQL IN()有重复的行? [英] MySQL IN() with duplicate rows?

查看:598
本文介绍了MySQL IN()有重复的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以这样创建MySQL查询:

Is it possible to create a MySQL query like this:

SELECT id, name FROM products WHERE id IN ( 36, 36, 34, 2 )

因此,因为id'36'被两次包含,所以它在结果中产生了该产品的两行?

So that because the id '36' is included twice, it produces two rows of that product in the result?

即结果集可能是:

id    name
36    Cool Product
36    Cool Product
34    Lame Product
 2    Mediocre Product

代替:

id    name
36    Cool Product
34    Lame Product
 2    Mediocre Product

或者可能将其视为数量",例如:

Or possibly to consider it as a 'quantity', like:

id    name                qty
36    Cool Product        2
34    Lame Product        1
 2    Mediocre Product    1

感谢您的帮助!

推荐答案

SELECT id, name, COUNT(name) AS qty
FROM products p
WHERE p.id IN ( 36, 34, 2 )
GROUP BY id, name

也就是说,假设您的产品表中有重复的记录,这似乎是错误的.

That said, that is assuming your products table has duplicate records which seems wrong.

这篇关于MySQL IN()有重复的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆