内部联接使用if语句,组

时间:2011-05-28 02:38:07

标签: mysql coldfusion

我这里有2组代码。 这两组代码正在运行,没有错误。 但是我遇到了一个问题,我需要将这两组代码分组。 请看一下,谢谢 这是我创建的第一个代码。

SELECT r.Name , r.Restaurant_ID, f.feature, r.Price_Range, r.Cuisine_ID, c.Cuisine,
    s.State_ID, s.State, l.Location_ID, l.Area, l.State_ID, r.Name, r.Location_ID
FROM Restaurants r, Bridge1_Restaurant_Features b, Features f, Cuisine c, State s, Location l
        where 0=0
        AND b.Feature_ID = f.Feature_ID 
        AND b.Restaurant_ID = r.Restaurant_ID
        AND r.Cuisine_ID = c.Cuisine_ID 
        AND r.Location_ID = l.Location_ID
        AND l.State_ID = s.State_ID
        <cfif ARGUMENTS.Feature_ID IS NOT "">
        AND f.Feature_ID IN (#ARGUMENTS.Feature_ID#)
        </cfif>
        <cfif ARGUMENTS.Price_Range IS NOT "">
        AND r.Price_Range IN (#ARGUMENTS.Price_Range#)
        </cfif>
        <cfif ARGUMENTS.Cuisine IS NOT "">
        AND r.Cuisine_ID = (#ARGUMENTS.Cuisine#)
        </cfif>
        <cfif val(ARGUMENTS.LocationID2) IS #val(ARGUMENTS.StateID)#>
        AND l.State_ID = #val(ARGUMENTS.LocationID2)#
        <cfelse>
            AND l.Location_ID = #val(ARGUMENTS.LocationID2)#
        </cfif>

稍后,我注意到feature_ID我需要使用另一个逻辑来显示结果。 代码就像这样

SELECT r.Restaurant_ID, r.Name, f.Feature
FROM   Restaurants r
INNER JOIN Bridge1_Restaurant_Features b ON b.Restaurant_ID = r.Restaurant_ID
INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
INNER JOIN
(
     SELECT Restaurant_ID, COUNT(Feature_ID) AS FeatureCount
     FROM   Bridge1_Restaurant_Features
     <!--- find matching features --->
     WHERE  Feature_ID IN ( <cfqueryparam value="#ARGUMENTS.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
    GROUP BY Restaurant_ID
    <!--- having ALL of the requested features --->
     HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(ARGUMENTS.Feature_ID)#" cfsqltype="cf_sql_integer">
) ck ON ck.Restaurant_ID = r.Restaurant_Id

我希望将这两个组合在一起。 第二组必须替换

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

我尝试将这两个代码分组,但未能成功。我尝试的代码在下面,它得到错误。

SELECT r.Name , r.Restaurant_ID, f.feature, r.Price_Range, r.Cuisine_ID, c.Cuisine,
        s.State_ID, s.State, l.Location_ID, l.Area, l.State_ID,  r.Location_ID
        FROM Restaurants r, Features f, Cuisine c, State s, Location l
        INNER JOIN Bridge1_Restaurant_Features b ON b.Restaurant_ID = r.Restaurant_ID
        INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
        AND r.Cuisine_ID = c.Cuisine_ID 
        AND r.Location_ID = l.Location_ID
        AND l.State_ID = s.State_ID
        <cfif ARGUMENTS.Feature_ID IS NOT "">
        INNER JOIN
        (
             SELECT Restaurant_ID, COUNT(Feature_ID) AS FeatureCount
             FROM   Bridge1_Restaurant_Features
             <!--- find matching features --->
             WHERE  Feature_ID IN ( <cfqueryparam value="#ARGUMENTS.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
            GROUP BY Restaurant_ID
            <!--- having ALL of the requested features --->
             HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(ARGUMENTS.Feature_ID)#" cfsqltype="cf_sql_integer">
        ) ck ON ck.Restaurant_ID = r.Restaurant_Id
        </cfif>
        <cfif ARGUMENTS.Price_Range IS NOT "">
        AND r.Price_Range IN (#ARGUMENTS.Price_Range#)
        </cfif>
        <cfif ARGUMENTS.Cuisine IS NOT "">
        AND r.Cuisine_ID = (#ARGUMENTS.Cuisine#)
        </cfif>
        <cfif val(ARGUMENTS.LocationID2) IS #val(ARGUMENTS.StateID)#>
        AND l.State_ID = #val(ARGUMENTS.LocationID2)#
        <cfelse>
            AND l.Location_ID = #val(ARGUMENTS.LocationID2)#
        </cfif>

2 个答案:

答案 0 :(得分:0)

我仍然不确定你想要什么。

这样的事情?

WITH group2 AS (

    SELECT r.Restaurant_ID, r.Name, f.Feature
    FROM   Restaurants r
    INNER JOIN Bridge1_Restaurant_Features b ON b.Restaurant_ID = r.Restaurant_ID
    INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
    INNER JOIN
    (
         SELECT Restaurant_ID, COUNT(Feature_ID) AS FeatureCount
         FROM   Bridge1_Restaurant_Features
         <!--- find matching features --->
         WHERE  Feature_ID IN ( <cfqueryparam value="#ARGUMENTS.Feature_ID#"   cfsqltype="cf_sql_integer" list="true"> )
        GROUP BY Restaurant_ID
        <!--- having ALL of the requested features --->
         HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(ARGUMENTS.Feature_ID)#" cfsqltype="cf_sql_integer">
        ) ck ON ck.Restaurant_ID = r.Restaurant_Id

)

SELECT r.Name , r.Restaurant_ID, f.feature, r.Price_Range, r.Cuisine_ID, c.Cuisine,
    s.State_ID, s.State, l.Location_ID, l.Area, l.State_ID, r.Name, r.Location_ID
FROM Restaurants r, Bridge1_Restaurant_Features b, Features f, Cuisine c, State s, Location l
    where 0=0
    AND b.Feature_ID = f.Feature_ID 
    AND b.Restaurant_ID = r.Restaurant_ID
    AND r.Cuisine_ID = c.Cuisine_ID 
    AND r.Location_ID = l.Location_ID
    AND l.State_ID = s.State_ID
    <cfif ARGUMENTS.Feature_ID IS NOT "">
    AND f.Feature_ID IN (group2.Restaurant_ID)
    </cfif>
    <cfif ARGUMENTS.Price_Range IS NOT "">
    AND r.Price_Range IN (#ARGUMENTS.Price_Range#)
    </cfif>
    <cfif ARGUMENTS.Cuisine IS NOT "">
    AND r.Cuisine_ID = (#ARGUMENTS.Cuisine#)
    </cfif>
    <cfif val(ARGUMENTS.LocationID2) IS #val(ARGUMENTS.StateID)#>
    AND l.State_ID = #val(ARGUMENTS.LocationID2)#
    <cfelse>
        AND l.Location_ID = #val(ARGUMENTS.LocationID2)#
    </cfif>

答案 1 :(得分:0)

我自己解决问题。如果有任何改进方法请告诉我,谢谢大家。

SELECT r.Restaurant_ID, r.Name, f.Feature, r.Price_Range, r.Cuisine_ID, c.Cuisine,
                l.Location_ID, l.Area, s.State
        FROM   Restaurants r
        <cfif ARGUMENTS.Feature_ID IS NOT "">
        INNER JOIN
        (
             SELECT Restaurant_ID, COUNT(Feature_ID) AS FeatureCount
             FROM   Bridge1_Restaurant_Features
             <!--- find matching features --->
             WHERE  Feature_ID IN ( <cfqueryparam value="#ARGUMENTS.Feature_ID#" cfsqltype="cf_sql_integer" list="true"> )
            GROUP BY Restaurant_ID
            <!--- having ALL of the requested features --->
             HAVING COUNT(Feature_ID) = <cfqueryparam value="#listLen(ARGUMENTS.Feature_ID)#" cfsqltype="cf_sql_integer">
        ) ck ON ck.Restaurant_ID = r.Restaurant_Id
        </cfif>
        INNER JOIN Location l ON r.Location_ID = l.Location_ID
        INNER JOIN State s ON l.State_ID = s.State_ID
        INNER JOIN Cuisine c ON r.Cuisine_ID = c.Cuisine_ID 
        INNER JOIN Bridge1_Restaurant_Features b ON b.Restaurant_ID = r.Restaurant_ID
        INNER JOIN Features f ON b.Feature_ID = f.Feature_ID
        <cfif ARGUMENTS.Cuisine IS NOT "">
        AND r.Cuisine_ID = (#ARGUMENTS.Cuisine#)
        </cfif>
        <cfif ARGUMENTS.Price_Range IS NOT "">
        AND r.Price_Range IN (#ARGUMENTS.Price_Range#)
        </cfif>
        <cfif val(ARGUMENTS.LocationID2) IS #val(ARGUMENTS.StateID)#>
        AND l.State_ID = #val(ARGUMENTS.LocationID2)#
        <cfelse>
            AND l.Location_ID = #val(ARGUMENTS.LocationID2)#
        </cfif>