如果没有记录,则获取默认值

时间:2018-07-02 07:14:49

标签: sql sql-server

我有一个连接两个表的sql查询。

   SELECT f.ProductSpecificationID, s.PropertyName, f.Value 
     FROM [LSCDATA].[ProductProperty] f 
LEFT JOIN [LSCMASTER].Property s 
       ON f.PropertyID=s.PropertyID
    WHERE s.PropertyName in ('spec_category','planning_method','inv_planning_method','so_source_type')
      AND f.ProductSpecificationID = 398431
 ORDER By ProductSpecificationID

,结果显示如下

ProductSpecificationID  PropertyName        Value
398431                  planning_method     MRP and MPP planning
398431                  so_source_type      INTERNAL
398431                  inv_planning_method Not planned
398431                  spec_category       GBPA Established

这是正确的,因为记录398431具有四个用于四个属性的值。

但是对于记录398432,它只有四个属性的三个值, 并显示类似该记录没有spec_category属性。

ProductSpecificationID  PropertyName        Value
398432                  planning_method     Not planned
398432                  so_source_type      INTERNAL
398432                  inv_planning_method Not planned

我只需要为spec_category换一行,其值为null

ProductSpecificationID  PropertyName        Value
398432                  planning_method     Not planned
398432                  so_source_type      INTERNAL
398432                  inv_planning_method Not planned
398432                  spec_category       Null

更新#1:

我创建了一个SQLFiddle,

http://sqlfiddle.com/#!18/a1bc4/4

我只需要另一行,仅spec_category的值为Null

3 个答案:

答案 0 :(得分:4)

我不了解数据结构。 在这里,您去了:

 try {
                            JSONObject data = new JSONObject(result);
                            int id = data.getInt("id");
                            String name=data.getString("name");
                            JSONArray list_question=data.getJSONArray("questions");
                            int length=list_question.length();
                            for (int i=0;i<length;i++){
                                JSONObject question=list_question.getJSONObject(i);
                                int question_id=question.getInt("id");
                                int test=question.getInt("test");
                                String question_text=question.getString("text");
                                JSONArray list_answer=question.getJSONArray("answers");
                                int lenght_answer=list_answer.length();
                                for (int j=0;i<lenght_answer;i++){
                                    JSONObject answer=list_answer.getJSONObject(j);

                                    int answer_id=answer.getInt("id");
                                    int que=answer.getInt("question");
                                    String answer_text=answer.getString("text");
                                    int status=answer.getInt("correct");



                                }

                            }
                        }catch (JSONException ex){}

答案 1 :(得分:0)

要获得真实的LEFT JOIN结果,请将右侧表格条件从WHERE移至ON

要获取NA而不是null,请使用COALESCE()

SELECT f.ProductSpecificationID, COELESCE(s.PropertyName, 'NA'), f.Value
FROM [LSCDATA].[ProductProperty] f 
LEFT JOIN [LSCMASTER].Property s ON f.PropertyID = s.PropertyID
AND s.PropertyName in
    ('spec_category','planning_method','inv_planning_method','so_source_type')
ORDER BY ProductSpecificationID

答案 2 :(得分:0)

使用cross join生成所需的所有行,然后使用left join引入现有值:

SELECT ps.ProductSpecificationID, 
       v.PropertyName,
       f.Value
FROM (VALUES ('spec_category'), 
             ('planning_method'), 
             ('inv_planning_method'),
             ('so_source_type')
     ) v(PropertyName) CROSS JOIN
     (SELCT DISTINCT f.ProductSpecificationID
      FROM [LSCDATA].[ProductProperty] f 
     ) ps LEFT JOIN
     [LSCMASTER].Property s
     ON s.PropertyID = ps.PropertyID AND
        s.PropertyName = v.PropertyName
ORDER BY ps.ProductSpecificationID