Last active
May 18, 2020 10:53
-
-
Save kovid-r/bc12aac4e501864f8642d4d42ecb80ff to your computer and use it in GitHub Desktop.
Select Non-repeating Planet/Satellite Combinations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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