如果每个对象/项目的个数(4)以上,则删除关系

时间:2018-12-16 12:49:43

标签: sql postgresql

我有2个实体项目和图片。一个项目可以有多个图像。

如果某件商品有多于4张图片,我想按ID顺序删除上面的图片

结构:

Item

id | name
1    a
2    b
3    c

Image

id | item_id
1    1
2    1 
3    2

2 个答案:

答案 0 :(得分:1)

您可以使用:

var json_data = {"headers":["FirstValue","SecondValue","ThirdValue","FourthValue","FifthValue","NumberOne","NumberTwo"],"rows":[["_2222_","_first_","CPV","unknown","_2222_",92310.8671,5226.386074028007],["_2222_","_first_","WWW","known","_2222_",92310.8671,5226.386074028007],["_2222_","_second_","VVV","unknown","_2222_",92310.8671,null],["_2222_","_third_","VVV","unknown","_2222_",92310.8671,5226.386074028007],["_1111_","_first_","VVV","unknown","random",197977.2658,36169.2693355163],["_1111_","_first_","WWW","unknown","random",197977.2658,36169.2693355163],["_1111_","_second_","VVV","unknown","random",197977.2658,36169.2693355163],["_1111_","_third_","WWW","display","random",26536.9836,1957.2823939366558]]};
 
var dataRows = json_data.rows;
var dataHeaders = json_data.headers;
var json =[];

//Convert JSON to dictionary
for (i = 0; i < dataRows.length; i++) 
{
  var object = {};
  for (j = 0; j < dataHeaders.length; j++) 
  {  
    object[dataHeaders[j]] = dataRows[i][j];
  }
  json.push(object);
}

var res = json.reduce((r, e) => {
  let {SecondValue, FirstValue} = e;
  r[FirstValue] = r[FirstValue] || {}
  r[FirstValue][SecondValue] = r[FirstValue][SecondValue] || {}
  r[FirstValue][SecondValue] = r[FirstValue][SecondValue] || {}
  Object.assign(r[FirstValue][SecondValue], e);
  return r;
}, {})

var array = Object.values(res).map(Object.values)

//Print the _1111_ branch
if("_1111_" in res){
	var _1111_ = res._1111_;
}

    //Print the _1111_ branch
    if("_first_" in _1111_){
      var _first_ = _1111_._first_;
      document.getElementById("one").innerHTML = JSON.stringify(_first_, null, 2);
    }
/*   
     if("_second_" in _1111_){
      var _second_ = _1111_._second_;
      document.getElementById("two").innerHTML = JSON.stringify(_second_, null, 2);
    }   

    if("_third_" in _1111_){
      var _third_ = _1111_._third_;
      document.getElementById("three").innerHTML = JSON.stringify(_third_, null, 2);
    }
*/
/*
//Print the _2222_ branch
if("_2222_" in res){
	var _2222_ = res._2222_;
} 

    //Print the _2222_ branch
    if("_first_" in _2222_){
      var _first_ = _2222_._first_;
      document.getElementById("four").innerHTML = JSON.stringify(_first_, null, 2);
    }

    if("_second_" in _2222_){
      var _second_ = _2222_._second_;
      document.getElementById("five").innerHTML = JSON.stringify(_second_, null, 2);
    }

    if("_third_" in _2222_){
      var _third_ = _2222_._third_;
      document.getElementById("six").innerHTML = JSON.stringify(_third_, null, 2);
    }
*/

在运行<h3>_1111_ - _first_</h3> <div style="background:grey;" id="one"></div> <!-- <h3>_1111_ - _second_</h3> <div style="background:green;" id="two"></div> <h3>_1111_ - _third_</h3> <div style="background:grey;" id="three"></div> <h3>_2222_ - _first_</h3> <div style="background:green;" id="four"></div> <h3>_2222_ - _second_</h3> <div style="background:grey;" id="five"></div> <h3>_2222_ - _third_</h3> <div style="background:green;" id="six"></div> -->之前,请检查子查询是否返回了所需的ID。

答案 1 :(得分:1)

我会做的:

delete from image
where id < (select i2.id
            from image i2
            where i2.item_id = i.item_id
            order by id desc
            limit 1 offset 3
           );

这将删除给定id小于第四大id的所有item_id