如何从MySQL数据库获取正确的复选框结果?

发布于 2024-11-10 03:22:45 字数 1256 浏览 1 评论 0原文

我这里有3张桌子。

1)Hotel

-----------------
|Hotel_ID | Name     |
-----------------
|    1    |Shangrila |
----------------------
|    2    |GoldHill  |
----------------------
|    3    |BayBeach  |
----------------------

2)Feature

----------------------
|Feature_ID| Feature |
----------------------
|     1    |  Goft   |
----------------------
|     2    |Internet |
----------------------

3)Brdige_Hotel_Feature

------------------------
|Hotel_ID | Feature_ID |
------------------------
|    1    |      1     |
------------------------
|    1    |      2     |
-----------------------
|     2   |      1     |
-----------------------

这意味着每个酒店可能有超过 1 个功能。

我的想法是这样的,假设我想从表3 Bridge_Hotel_Feature 中获取结果。 如果 Feature_ID = 1 ,我得到 酒店1和2。 **如果Feature_ID = 1, 2。我只想得到 酒店 1. 但我总是得到 Hotel_ID 1 和 2。 ** 请帮助我找到仅获取与 Hotel_ID 匹配的功能的解决方案。

下面是我尝试的代码。

SELECT h.Name , h.Hotel_ID, f.feature
FROM Hotel h, Bridge1_Hotel_Features b, Features f
where 0=0
AND b.Feature_ID = f.Feature_ID 
AND b.Hotel_ID = r.Hotel_ID

<cfif #FORM.Feature_ID# IS NOT "">
    AND f.Feature_ID IN (#FORM.Feature_ID#)
</cfif>

I have 3 table here.

1)Hotel

-----------------
|Hotel_ID | Name     |
-----------------
|    1    |Shangrila |
----------------------
|    2    |GoldHill  |
----------------------
|    3    |BayBeach  |
----------------------

2)Feature

----------------------
|Feature_ID| Feature |
----------------------
|     1    |  Goft   |
----------------------
|     2    |Internet |
----------------------

3)Brdige_Hotel_Feature

------------------------
|Hotel_ID | Feature_ID |
------------------------
|    1    |      1     |
------------------------
|    1    |      2     |
-----------------------
|     2   |      1     |
-----------------------

It mean each hotel might have more than 1 feature.

My idea is like this , let say, if i want get the result from table 3 Bridge_Hotel_Feature.
If the Feature_ID = 1 , i get
Hotel 1 and 2.
**If the Feature_ID = 1 , 2. I just want to get
Hotel 1.
But i always get the both Hotel_ID 1 and 2. **
Please help me the solution to get only the feature match with the Hotel_ID.

Below is the code i try.

SELECT h.Name , h.Hotel_ID, f.feature
FROM Hotel h, Bridge1_Hotel_Features b, Features f
where 0=0
AND b.Feature_ID = f.Feature_ID 
AND b.Hotel_ID = r.Hotel_ID

<cfif #FORM.Feature_ID# IS NOT "">
    AND f.Feature_ID IN (#FORM.Feature_ID#)
</cfif>

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

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

发布评论

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

评论(3

彼岸花ソ最美的依靠 2024-11-17 03:22:45

如果Feature_ID = 1 ,2 ,结果必须
获取Hotel_ID = 1

因为只有 Hotel_ID = 1 具有这两个功能,对吗?假设 #FORM.Feature_ID# 不包含重复项,请使用 HAVING 子句动态标识具有所有请求功能的酒店。

SELECT Hotel_ID, COUNT(Feature_ID) AS FeatureCount
FROM   Bridge_Hotel_Feature
<!--- find matching features --->
WHERE  Feature_ID IN ( <cfqueryparam value="#FORM.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
GROUP BY Hotel_ID
<!--- having ALL of the requested features --->
HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(FORM.Feature_ID)#" cfsqltype="cf_sql_integer">

然后,您可以将其作为派生表或可能的子查询连接到它。 sql 需要优化,但概念上类似于

SELECT h.Hotel_ID, h.Name, f.Feature
FROM   Hotel h
      INNER JOIN Bridge_Hotel_Feature b ON b.Hotel_ID = h.Hotel_ID
      INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
      INNER JOIN
      (
        SELECT Hotel_ID, COUNT(Feature_ID) AS FeatureCount
        FROM   Bridge_Hotel_Feature
        <!--- find matching features --->
        WHERE  Feature_ID IN ( <cfqueryparam value="#FORM.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
        GROUP BY Hotel_ID
        <!--- having ALL of the requested features --->
        HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(FORM.Feature_ID)#" cfsqltype="cf_sql_integer">
    ) ck ON ck.Hotel_ID = h.Hotel_Id

If Feature_ID = 1 ,2 , the result must
only get Hotel_ID = 1

Because only Hotel_ID = 1 has both features, correct? Assuming #FORM.Feature_ID# does not contain duplicates, use a HAVING clause to dynamically identify hotels with all of the requested features.

SELECT Hotel_ID, COUNT(Feature_ID) AS FeatureCount
FROM   Bridge_Hotel_Feature
<!--- find matching features --->
WHERE  Feature_ID IN ( <cfqueryparam value="#FORM.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
GROUP BY Hotel_ID
<!--- having ALL of the requested features --->
HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(FORM.Feature_ID)#" cfsqltype="cf_sql_integer">

You could then join to it as a derived table or possibly a subquery. The sql needs optimization, but conceptually something like

SELECT h.Hotel_ID, h.Name, f.Feature
FROM   Hotel h
      INNER JOIN Bridge_Hotel_Feature b ON b.Hotel_ID = h.Hotel_ID
      INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
      INNER JOIN
      (
        SELECT Hotel_ID, COUNT(Feature_ID) AS FeatureCount
        FROM   Bridge_Hotel_Feature
        <!--- find matching features --->
        WHERE  Feature_ID IN ( <cfqueryparam value="#FORM.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
        GROUP BY Hotel_ID
        <!--- having ALL of the requested features --->
        HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(FORM.Feature_ID)#" cfsqltype="cf_sql_integer">
    ) ck ON ck.Hotel_ID = h.Hotel_Id
假情假意假温柔 2024-11-17 03:22:45

将 sql 更改为:

SELECT h.Name , h.Hotel_ID, f.feature
FROM Hotel h, Bridge1_Hotel_Features b, Features f
where b.Feature_ID = f.Feature_ID 
      AND b.Hotel_ID = h.Hotel_ID

<cfif #FORM.Feature_ID# IS NOT "">
    AND b.Feature_ID IN (#FORM.Feature_ID#)
</cfif>

本质上,where 子句的可选部分应该限制 Bridge_Hotel_Features 表中的 feature_id。

Change sql to:

SELECT h.Name , h.Hotel_ID, f.feature
FROM Hotel h, Bridge1_Hotel_Features b, Features f
where b.Feature_ID = f.Feature_ID 
      AND b.Hotel_ID = h.Hotel_ID

<cfif #FORM.Feature_ID# IS NOT "">
    AND b.Feature_ID IN (#FORM.Feature_ID#)
</cfif>

Essentially, the optional part of where clause should be restricting the feature_id in the Bridge_Hotel_Features table.

意犹 2024-11-17 03:22:45

你需要使用EXIST来解决这个问题。

SELECT h.Name , h.Hotel_ID, f.feature
FROM Hotel h, Bridge_Hotel_Feature b, Feature f
where b.Feature_ID = f.Feature_ID 
      AND b.Hotel_ID = h.Hotel_ID
AND EXISTS (SELECT feature_id FROM bridge_hotel_feature WHERE feature_id = 1 AND bridge_hotel_feature.hotel_ID = h.hotel_ID)
AND EXISTS (SELECT feature_id FROM bridge_hotel_feature WHERE feature_id = 2 AND bridge_hotel_feature.hotel_ID = h.hotel_ID)

编辑:以上是您的查询需要的示例。为了使其动态化,您将添加一个循环。

我自己没有 Coldfusion 经验,所以我无法逐字告诉您如何处理该代码。

但您需要做的是将以下代码部分包装在循环中,并将其附加到每个复选框的查询字符串中,并将 where 子句中的 feature_id 替换为每个复选框的 feature_id。
AND EXISTS (SELECT feature_id FROMbridge_hotel_feature WHERE feature_id = 1 ANDbridge_hotel_feature.hotel_ID = h.hotel_ID)

我希望这能让您更清楚。

You need to use EXIST to solve this problem.

SELECT h.Name , h.Hotel_ID, f.feature
FROM Hotel h, Bridge_Hotel_Feature b, Feature f
where b.Feature_ID = f.Feature_ID 
      AND b.Hotel_ID = h.Hotel_ID
AND EXISTS (SELECT feature_id FROM bridge_hotel_feature WHERE feature_id = 1 AND bridge_hotel_feature.hotel_ID = h.hotel_ID)
AND EXISTS (SELECT feature_id FROM bridge_hotel_feature WHERE feature_id = 2 AND bridge_hotel_feature.hotel_ID = h.hotel_ID)

EDIT: The above is an example of what your query needs to look like. To make it dynamic you will add a loop.

I don't have coldfusion experience myself so I can't tell you verbatim what to do with that code.

But what you need to do is wrap the following section of code in a loop and append this to your query string for each checkbox and replace the feature_id in the where clause with the feature_id of each checkbox.
AND EXISTS (SELECT feature_id FROM bridge_hotel_feature WHERE feature_id = 1 AND bridge_hotel_feature.hotel_ID = h.hotel_ID)

I hope this makes it clearer for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文