如何从MySQL数据库获取正确的复选框结果?
我这里有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为只有 Hotel_ID = 1 具有这两个功能,对吗?假设
#FORM.Feature_ID#
不包含重复项,请使用 HAVING 子句动态标识具有所有请求功能的酒店。然后,您可以将其作为派生表或可能的子查询连接到它。 sql 需要优化,但概念上类似于
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.You could then join to it as a derived table or possibly a subquery. The sql needs optimization, but conceptually something like
将 sql 更改为:
本质上,where 子句的可选部分应该限制 Bridge_Hotel_Features 表中的 feature_id。
Change sql to:
Essentially, the optional part of where clause should be restricting the feature_id in the Bridge_Hotel_Features table.
你需要使用EXIST来解决这个问题。
编辑:以上是您的查询需要的示例。为了使其动态化,您将添加一个循环。
我自己没有 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.
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.