怎么写这个查询? [英] How to write this query?

查看:83
本文介绍了怎么写这个查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个名为 ProductLike(ProductId,UserId,Like)的表。每当用户喜欢某个产品时,就会在此表中添加一条记录,其中包含 UserId ProductId (该产品很受欢迎)和喜欢(真或假)。

我想从这里获得 usersId ProductLike 表喜欢一种产品的表。更确切地说,我希望得到一个列表或一组用户,他们对每种产品都喜欢相同的产品。



请指导如何编写查询。



我正在写这个,但它有错误:

Hi,
I have a table Named ProductLike(ProductId, UserId, Like). Whenever user likes a product then a record is added in this table with UserId, ProductId(which product is liked) and Like(true or false).
I would like to get such usersId from the ProductLike table who has liked one product. To be more precise, I want to get a list or group of users who has liked same product for each product.

Please guide How can I write a query.

I am writing this one but it has errors:

select p.UserId,* from ProductLike p where p.ProductId = (select pk.ProductId from ProductLike pk
where pk.ProductLike=1)



请指导,因为我的查询不是解决方案,只是提示。


Please guide as my query is not a solution it is just hint.

推荐答案

SELECT ProductId, UserId
FROM ProductLike
WHERE [Like] = 1
ORDER BY ProductId, UserId





如果你想要一份清单特定产品(例如 ProductId 1)添加

AND ProductId = 1

WHERE 子句。



请注意保留关键字 [ ^ ]和你一起可能想为该字段使用另一个名称。

to the WHERE clause.

Please note that Like is a reserved keyword[^] and you might want to use another name for that field.


不确定我是否正确理解了这个问题,但你提到了组。因此,如果您需要每个产品以及有多少用户喜欢它,则查询可能类似于:

Not sure if I understood the question correctly, but you mentioned group. So if you need each product and how many users liked it, the query could be something like:
SELECT pl.ProductId,
       COUNT(*) AS NumberOfLikes
FROM   ProductLike pl
WHERE pl.[Like] = True
GROUP BY pl.ProductId


不确定我是否'你在问题中遗漏了一些东西,但我认为这样做会有所作为。你将获得一个包含部分所有产品的结果集。

Not sure if I''m missing somethin in your question, but I would think this would do the job. You''ll get a result set that contains all the products in sections.
SELECT ProductId, UserId
  FROM ProductLike
 WHERE Like = 1
 ORDER BY ProductId, UserId


这篇关于怎么写这个查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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