MySQL 单行多记录

发布于 2024-11-14 02:15:46 字数 253 浏览 10 评论 0原文

我有一个表,其中包含多行,这些行定义了特定度假村的“设施”。如果有包含我定义的“amenOptionID”的行,我需要返回resortID。我的问题在于我想看看度假村是否有两个或更多设施。例如:

我想返回同时具有 amenOptionID 1 和 4 的 ResortID。我也不想要重复的 ResortID。表结构请参阅图片。预先感谢您的任何帮助。

表结构

I have a table with contains multiple rows that define "amenities" for a particular resort. I need to return the resortID if there are rows containing whatever "amenOptionID" I define. My issue comes in where I'm looking to see if a resort has two or more amenities. For example:

I want to return resortIDs that have BOTH amenOptionID 1 AND 4. I also do not want duplicate resortIDs. Refer to the image for the table structure. Thanks in advance for any help.

Table Structure

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芯好空 2024-11-21 02:15:46
  SELECT `resortID`
   WHERE `amenOptionID`
         IN (1, 4) 
GROUP BY `resortID`
  HAVING COUNT(*) = 2
  SELECT `resortID`
   WHERE `amenOptionID`
         IN (1, 4) 
GROUP BY `resortID`
  HAVING COUNT(*) = 2
番薯 2024-11-21 02:15:46

我认为这会起作用...

SELECT resortID, COUNT(*) AS theCount FROM myTable WHERE amenOptionID IN(1,4) GROUP BY resortId HAVING theCount=2;

I think this will work...

SELECT resortID, COUNT(*) AS theCount FROM myTable WHERE amenOptionID IN(1,4) GROUP BY resortId HAVING theCount=2;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文