clear markdown compare help best fiddles feedback dbanow.uk a leap of faith?
clear markdown donate comments/suggestions/bugs a leap of faith? bible365 diddy dollings
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 1130830 fiddles created (16570 in the last week).

create table table1(id integer, num integer); insert into table1(id, num) values(1, 3),(2, 5),(3, 6); SELECT * FROM table1; create table table2(uni_id integer, id integer); insert into table2(uni_id, id) values(1, 1),(2, 2),(3, 2),(4, 2),(5, 3); SELECT * FROM table2; create table table3(uni_id integer); insert into table3(uni_id) values(1),(2),(3),(5); SELECT * FROM table3;
id num
1 3
2 5
3 6
uni_id id
1 1
2 2
3 2
4 2
5 3
uni_id
1
2
3
5
 hidden batch(es)


-- Критерий поиска SET @number := 5;
 hidden batch(es)


-- выборка из таблицы 1 SELECT t1.id FROM table1 t1 WHERE t1.num = @number;
id
2
 hidden batch(es)


-- выборка соотв. записей из таблицы 2 SELECT t2.uni_id FROM table1 t1 JOIN table2 t2 USING (id) WHERE t1.num = @number;
uni_id
2
3
4
 hidden batch(es)


-- выборка нужных данных из таблицы 3 SELECT t3.uni_id FROM table1 t1 JOIN table2 t2 USING (id) JOIN table3 t3 USING (uni_id) WHERE t1.num = @number ORDER BY t3.uni_id DESC LIMIT 1;
uni_id
3
 hidden batch(es)