GordonLinoffさん@伴う問題それは、チケットテーブルに最大エントリを持つクライアントの数を事前に知る必要があるということです。データが1,2,3,4,4,2,3になり、その後1,2,3,4,4,2,3,4になったときにどうなるでしょう。 OPの初期文章によれば、最初のものの後の結果はid = 2,3,4であり、2番目のid = 4の後の結果である。必要なのは、SQL内で必要な数を決定するソリューションです。以下はそのことです。残念ながら、それはOracleのソリューションです。私はMySqlについてよくわかりませんが、以下のすべての構造を持っていると思います。
メインクエリ:
/* Final desirded columns from clients */
select name, surname
from clients
where 1=1
and id_client in
/* get id_client from ticket and assign a unique row_number for each */
(select id_client
from (select id_client
, row_number() over(partition by id_client
order by id_client) rn
from tickets
)
where 1=1
/* select only those having assigned row numbers */
and rn =
/* get the maximum rows for any id_client */
(select max(n)
from (select count(*) over(partition by id_client) n
from tickets)));
create table clients (id_client integer, name varchar2(50), surname varchar2(30));
create table tickets (id_ticket integer, id_client integer);
insert into clients (id_client, name, surname) values (1.,'Bob','Meyers');
insert into clients (id_client, name, surname) values (2.,'Bob','Adams');
insert into clients (id_client, name, surname) values (3.,'Lynn','Adams');
insert into clients (id_client, name, surname) values (4.,'William','Wallice');
insert into clients (id_client, name, surname) values (5.,'Julius','Caesar');
insert into clients (id_client, name, surname) values (6.,'William', 'Shakespeare');
insert into clients (id_client, name, surname) values (7.,'Cleopatra', 'Philopator');
insert into tickets (id_ticket, id_client)
select rownum, t
from (select 1 t from dual union all
select 2 from dual union all
select 3 from dual union all
select 4 from dual union all
select 4 from dual union all
select 2 from dual
);
RUN MAIN QUERY。
insert into tickets (id_ticket, id_client) values ((select max(id_ticket) + 1 from tickets), 3);
ランメインクエリー。
insert into tickets (id_ticket, id_client) values ((select max(id_ticket) + 1 from tickets), 4);
ランメインクエリ。
drop table tickets;
drop table clients;
あなたにとっては、私は短くてよい解決策があると理解していますが、各ステップで派生を表示したかったのです。あなたのために、おそらく "LIMIT(select ...)"の行に沿ったMySqlのために、私は解決策に興味があります。
ありがとうございました!できます –