Update HSTORE record and then delete if it is now empty - What is the correct sql?
От | Ashwin Jayaprakash |
---|---|
Тема | Update HSTORE record and then delete if it is now empty - What is the correct sql? |
Дата | |
Msg-id | CAF9YjSA-wEdcbHitDw19jM6K=giGESiojJ08_coUcLmTHacj3g@mail.gmail.com обсуждение исходный текст |
Ответы |
Re: Update HSTORE record and then delete if it is now empty -
What is the correct sql?
Re: Update HSTORE record and then delete if it is now empty - What is the correct sql? |
Список | pgsql-sql |
Hi, here's what I'm trying to do:
- I have a table that has an HSTORE column
- I would like to delete some key-vals from it
- If after deleting key-vals, the HSTORE column is empty, I'd like to delete the entire row- I have a table that has an HSTORE column
- I would like to delete some key-vals from it
create table up_del(name varchar(256) primary key, data hstore);
insert into up_del(name, data) values
('aa', 'a=>123'),
('bb', 'b=>456, a=>456'),
('cc', 'c=>678'),
('dd', 'd=>901'),
('ee', '');
select * from up_del;
with update_qry as(
update up_del as r
set data = delete(data, 'c=>678')
where name = 'cc'
returning r.*
)
delete from up_del
where name in (select name from update_qry)
and array_length(akeys(data), 1) is null;
insert into up_del(name, data) values
('aa', 'a=>123'),
('bb', 'b=>456, a=>456'),
('cc', 'c=>678'),
('dd', 'd=>901'),
('ee', '');
select * from up_del;
with update_qry as(
update up_del as r
set data = delete(data, 'c=>678')
where name = 'cc'
returning r.*
)
delete from up_del
where name in (select name from update_qry)
and array_length(akeys(data), 1) is null;
with update_qry as(
update up_del as r
set data = delete(data, 'c=>678')
where name = 'cc' or name = 'ee'
returning r.*
)
select *, array_length(akeys(data), 1) is null from update_qry;
update up_del as r
set data = delete(data, 'c=>678')
where name = 'cc' or name = 'ee'
returning r.*
)
select *, array_length(akeys(data), 1) is null from update_qry;
Thanks,
Ashwin.
В списке pgsql-sql по дате отправления: