java 如何停止忽略“WHERE IN()”查询中的重复条目?

vddsk6oq  于 2023-04-04  发布在  Java
关注(0)|答案(2)|浏览(138)

我试图获取一个查询,该查询统计项目中某餐的特定宏观和微观成分。我有一个List<Long>的id,这些id表示特定的餐,我想使用该列表计算成分的组合值。问题是有些餐重复了(这没问题),但是当我将列表传递给我的SQL查询时,它忽略了重复的ID(例如,如果列表看起来像{1,3,4,1,5},它只会计算一次膳食ID 1的成分,我需要它不要忽视二审
我的查询如下所示:

SELECT s.nazwa, SUM(ps.wartosc * (pwp.wartosc / 100)/pwp.liczba_porcji_posilku) as ilosc_skladnika, s.jednostka, s.grupa_skladnikow_odzywczych
FROM produkty_skladniki ps
JOIN posilki_produkty pwp USING (produkt_id)
JOIN skladniki s USING (skladnik_id)
WHERE pwp.posilek_id IN (LIST FROM JAVA HERE)
GROUP BY skladnik_id, s.nazwa, s.jednostka, s.grupa_skladnikow_odzywczych, s.skladnik_id 
ORDER BY s.kolejnosc

和将列表传递给

public List<SimulatedMealInfo> checkNorms(List<Menu> menuList){
    //If there are records in DB remove them beforehand
    if(!simulatedMealInfoService.findAll().isEmpty()){
        simulatedMealInfoService.deleteAllCustom();
    }
    //Get the meal ID from each Menu class item
    List<Long> ids = new ArrayList<>();
    for(Menu item : menuList){
        ids.add(item.getMeal().getMealId());
    }
    //Create list of objects returned by query
    List<Object[]> mealObjectView = mealService.checkIngredientsOverall(ids);
    //Create list of objects that will hold needed info
    List<SimulatedMealInfo> mealInfo = new ArrayList<>();
    for(Object[] mealSecond : mealObjectView){
        Double d = (Double) mealSecond[1];
        //Create new objects using info returned by the query
        SimulatedMealInfo simulatedMealInfo = new SimulatedMealInfo(null,mealSecond[0].toString(),
                String.format("%.3f",d/7), mealSecond[2].toString(), mealSecond[3].toString());
        mealInfo.add(simulatedMealInfo);
    }
    //save dump
    simulatedMealInfoService.saveAll(mealInfo);
    return mealInfo;
}
zd287kbt

zd287kbt1#

根据您正在使用的数据库,有一种方法可以使用某些值声明临时表,使其在join中而不是in()中。可以连接的本地表的SQL Server示例:

SELECT * FROM (VALUES (1), (3), (4), (1), (5)) AS TempTable(ColumnName)
hmmo2u0o

hmmo2u0o2#

您可以修改Java代码以包含重复的膳食ID:

public List<SimulatedMealInfo> checkNorms(List<Menu> menuList){
    //If there are records in DB remove them beforehand
    if(!simulatedMealInfoService.findAll().isEmpty()){
        simulatedMealInfoService.deleteAllCustom();
    }
    //Get the meal ID from each Menu class item and duplicate them based on frequency
    List<Long> ids = new ArrayList<>();
    for(Menu item : menuList){
        long mealId = item.getMeal().getMealId();
        int frequency = Collections.frequency(ids, mealId);
        for (int i = 0; i < frequency; i++) {
            ids.add(mealId);
        }
    }
    //Create list of objects returned by query
    List<Object[]> mealObjectView = mealService.checkIngredientsOverall(ids);
    //Create list of objects that will hold needed info
    List<SimulatedMealInfo> mealInfo = new ArrayList<>();
    for(Object[] mealSecond : mealObjectView){
        Double d = (Double) mealSecond[1];
        //Create new objects using info returned by the query
        SimulatedMealInfo simulatedMealInfo = new SimulatedMealInfo(null,mealSecond[0].toString(),
                String.format("%.3f",d/7), mealSecond[2].toString(), mealSecond[3].toString());
        mealInfo.add(simulatedMealInfo);
    }
    //save dump
    simulatedMealInfoService.saveAll(mealInfo);
    return mealInfo;
}

SQL查询,该查询应计算列表中每次出现的每个膳食ID的成分:

SELECT s.nazwa, 
       SUM(ps.wartosc * (pwp.wartosc / 100)/pwp.liczba_porcji_posilku) as ilosc_skladnika, 
       s.jednostka, 
       s.grupa_skladnikow_odzywczych
FROM produkty_skladniki ps
JOIN posilki_produkty pwp USING (produkt_id)
JOIN skladniki s USING (skladnik_id)
WHERE pwp.posilek_id IN (?)
GROUP BY pwp.posilek_id, skladnik_id, s.nazwa, s.jednostka, s.grupa_skladnikow_odzywczych, s.skladnik_id 
ORDER BY s.kolejnosc

GROUP BY子句现在包含pwp.posilek_id列,它确保输入列表中每次出现膳食ID时都单独计算配料。?占位符表示一个通用参数,该参数将被从Java传递的膳食ID列表替换。

相关问题