select a.data, b.another_data
from block as a join block_one as b on (a.main_block_id=b.main_block_id)
where a.type = 'one'
union all
select a.data, b.third_data
from block as a join block_two as b on (a.main_block_id=b.main_block_id)
where a.type = 'two'
select distinct a.data,
(case when a.type='one' then b.another_data else c.third_data end) as some_data
from block as a left join block_one as b on (a.type = 'one' and a.main_block_id=b.main_block_id)
left join block_two as c on (a.type = 'two' and a.main_block_id=c.main_block_id)
SELECT DISTINCT a.name, b.position
FROM (SELECT name FROM employee) AS a CROSS JOIN (SELECT position FROM employee) AS b;
имен сотрудников некоторого отдела с разными должностями
select distinct a.id, a.value, (case when a.id_pc is not null then b.value when a.id_monitor is not null then c.value when a.id_printer is not null then d.value else null end) inv_num
from task a left join pc b on (a.id_pc=b.id)
left join monitor c on (a.id_monitor=c.id)
left join printer d on (a.id_printer=d.id)
CREATE TABLE animals (
grp ENUM('fish','mammal','bird') NOT NULL,
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (grp,id)
) ENGINE=MyISAM;
INSERT INTO animals (grp,name) VALUES
('mammal','dog'),('mammal','cat'),
('bird','penguin'),('fish','lax'),('mammal','whale'),
('bird','ostrich');
SELECT * FROM animals ORDER BY grp,id;
+--------+----+---------+
| grp | id | name |
+--------+----+---------+
| fish | 1 | lax |
| mammal | 1 | dog |
| mammal | 2 | cat |
| mammal | 3 | whale |
| bird | 1 | penguin |
| bird | 2 | ostrich |
+--------+----+---------+