有没有办法选择一个值,而其他相同的值转换为null

时间:2019-10-06 18:03:36

标签: mysql sql

我有3个连接的表,并且有一个查询要从中获取值。他们都有重复。现在,我需要2个表中的所有重复项,但是从第三个表中我只想获取第一个不同的值,而另一个要转换为NULL。

这是我的查询

 wt = prompt("Enter your weight"); //global wt variable
 ht = prompt("Enter your Height"); //global ht variable
// function bmi(wt, ht) function creating local variables wt,ht and masking the variables of the global scope
function bmi() //this will work
{
var bmiCal = (wt)/(ht * ht);
bmiCal = Math.round(bmiCal);
if (bmiCal<18.5)
{
    alert("Your BMI is " + bmiCal + "so you are underweight");
}
if (bmiCal >=18.5 && bmiCal <=24.9){
    alert("Your BMI is " + bmiCal + "so you are normal");
}
if (bmiCal >24.9){
    alert("Your BMI is  " + bmiCal + " so you are overweight");
}
}
bmi();

查看图片

Image of table



我也尝试过

function bmi() {
  let weight = prompt("Enter your weight"); // use let/const or var to create variables and keep them enclosed if you do not want them globally
  let height = prompt("Enter your height"); // use proper variable names
  let bmi = Math.round(weight / (height * height));
  if(bmi < 18.5)
    alert(`Your BMI is ${bmi}, so you are underweight`); //using string templates to remove multiple concatenation
  else if(bmi >= 18.5 && bmi <=24.9) // using else can reduce comparisons
    alert(`Your BMI is ${bmi}, so you are normal`); //using string 
  else if(bmi > 24.9)
    alert(`Your BMI is ${bmi}, so you are overweight`);
}

bmi();

1 个答案:

答案 0 :(得分:1)

如果您使用的是MySQL 8.0或更高版本,则可以通过LAG分析功能轻松实现-

SELECT operacije.nazivEng
      ,slike.img_name
      ,CASE WHEN izvestaji.operacija <> LAG(izvestaji.operacija) OVER(PARTITION BY operacije.nazivEng ORDER BY slike.img_name)
                 THEN izvestaji.operacija
            ELSE
                 NULL
       END operacija
FROM izvestaji 
INNER JOIN operacije ON izvestaji.operacijaId=operacije.id 
INNER JOIN slike ON izvestaji.id=slike.izvestajId 
WHERE izvestaji.operacija IN (SELECT operacija 
                              FROM izvestaji 
                              WHERE projekatId='8' 
                              AND datum='2019-10-03' 
                              GROUP BY operacija)
AND izvestaji.projekatId='8'
AND izvestaji.datum='2019-10-03' 
GROUP BY slike.img_name
        ,operacije.nazivEng
        ,izvestaji.operacija 
ORDER BY operacije.nazivEng DESC;