Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kovid-r/bc12aac4e501864f8642d4d42ecb80ff to your computer and use it in GitHub Desktop.
Save kovid-r/bc12aac4e501864f8642d4d42ecb80ff to your computer and use it in GitHub Desktop.
Select Non-repeating Planet/Satellite Combinations
create table devstronomy.temp_l_satellite like devstronomy.l_satellite;
alter table devstronomy.temp_l_satellite add primary key (id);
alter table devstronomy.temp_l_satellite add column ins_ts timestamp default current_timestamp not null;
insert ignore into devstronomy.temp_l_satellite
select ls.*, current_timestamp
from devstronomy.l_satellite ls
where planet_id = (select lp.id
from devstronomy.l_planet lp
order by rand()
limit 1)
and not exists (select 1
from devstronomy.temp_l_satellite tls
where tls.id = ls.id
limit 1
)
order by rand()
limit 1;
select *
from devstronomy.temp_l_satellite
order by ins_ts desc
limit 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment