Вот запрос
select * from
(
select 'T1' srctable,(select max(field) from T1) maxfield union
select 'T2' ,(select max(field) from T2) union
select 'T3' ,(select max(field) from T3)
) A WHERE maxfield =
(
select max(maxfield) from
(
select 'T1' srctable,(select max(field) from T1) maxfield union
select 'T2' ,(select max(field) from T2) union
select 'T3' ,(select max(field) from T3)
) AA
);
Вот ваши образцы данных
drop database if exists abidibo;
create database abidibo;
use abidibo
create table T1
(
id int not null auto_increment,
field int not null,
primary key (id),
key (field)
) ENGINE=MyISAM;
create table T2 LIKE T1;
create table T3 LIKE T1;
insert into T1 (field) values (6),(8),(23);
insert into T2 (field) values (8),(45),(23);
insert into T3 (field) values (68),(5),(67);
select * from T1;
select * from T2;
select * from T3;
Я загрузил ваши образцы данных
mysql> drop database if exists abidibo;
Query OK, 3 rows affected (0.00 sec)
mysql> create database abidibo;
Query OK, 1 row affected (0.02 sec)
mysql> use abidibo
Database changed
mysql> create table T1
-> (
-> id int not null auto_increment,
-> field int not null,
-> primary key (id),
-> key (field)
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.05 sec)
mysql> create table T2 LIKE T1;
Query OK, 0 rows affected (0.05 sec)
mysql> create table T3 LIKE T1;
Query OK, 0 rows affected (0.06 sec)
mysql> insert into T1 (field) values (6),(8),(23);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into T2 (field) values (8),(45),(23);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into T3 (field) values (68),(5),(67);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from T1;
+----+-------+
| id | field |
+----+-------+
| 1 | 6 |
| 2 | 8 |
| 3 | 23 |
+----+-------+
3 rows in set (0.00 sec)
mysql> select * from T2;
+----+-------+
| id | field |
+----+-------+
| 1 | 8 |
| 2 | 45 |
| 3 | 23 |
+----+-------+
3 rows in set (0.00 sec)
mysql> select * from T3;
+----+-------+
| id | field |
+----+-------+
| 1 | 68 |
| 2 | 5 |
| 3 | 67 |
+----+-------+
3 rows in set (0.00 sec)
mysql>
и вот результат моего запроса
mysql> select * from
-> (
-> select 'T1' srctable,(select max(field) from T1) maxfield union
-> select 'T2' ,(select max(field) from T2) union
-> select 'T3' ,(select max(field) from T3)
-> ) A WHERE maxfield =
-> (select max(maxfield) from
-> (
-> select 'T1' srctable,(select max(field) from T1) maxfield union
-> select 'T2' ,(select max(field) from T2) union
-> select 'T3' ,(select max(field) from T3)
-> ) AA);
+----------+----------+
| srctable | maxfield |
+----------+----------+
| T3 | 68 |
+----------+----------+
1 row in set (0.00 sec)
mysql>
ОБНОВЛЕНИЕ 2012-03-23 11:02 ПО ВОСТОЧНОМУ ВРЕМЕНИ
Я только что кое-что понял. Что если несколько таблиц имеют одинаковое максимальное значение?
Существует одна из трех функций, которые вы можете использовать, чтобы исправить, какую таблицу вы хотите видеть:
- MIN (первая таблица соответствия)
- MAX (последняя таблица соответствия)
- GROUP_CONCAT (все таблицы соответствия)
Я перезагружу ваши данные образца плюс положу 68 в T1
mysql> drop database if exists abidibo;
Query OK, 3 rows affected (0.01 sec)
mysql> create database abidibo;
Query OK, 1 row affected (0.00 sec)
mysql> use abidibo
Database changed
mysql> create table T1
-> (
-> id int not null auto_increment,
-> field int not null,
-> primary key (id),
-> key (field)
-> ) ENGINE=MyISAM;
Query OK, 0 rows affected (0.08 sec)
mysql> create table T2 LIKE T1;
Query OK, 0 rows affected (0.05 sec)
mysql> create table T3 LIKE T1;
Query OK, 0 rows affected (0.08 sec)
mysql> insert into T1 (field) values (6),(8),(23),(68);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> insert into T2 (field) values (8),(45),(23);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into T3 (field) values (68),(5),(67);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from T1;
+----+-------+
| id | field |
+----+-------+
| 1 | 6 |
| 2 | 8 |
| 3 | 23 |
| 4 | 68 |
+----+-------+
4 rows in set (0.00 sec)
mysql> select * from T2;
+----+-------+
| id | field |
+----+-------+
| 1 | 8 |
| 2 | 45 |
| 3 | 23 |
+----+-------+
3 rows in set (0.00 sec)
mysql> select * from T3;
+----+-------+
| id | field |
+----+-------+
| 1 | 68 |
| 2 | 5 |
| 3 | 67 |
+----+-------+
3 rows in set (0.00 sec)
mysql>
Теперь давайте выполним каждый запрос с MIN, MAX и GROUP_CONCAT
mysql> select min(srctable) srctables,maxfield from
-> (
-> select 'T1' srctable,(select max(field) from T1) maxfield union
-> select 'T2' ,(select max(field) from T2) union
-> select 'T3' ,(select max(field) from T3)
-> ) A WHERE maxfield =
-> (
-> select max(maxfield) from
-> (
-> select 'T1' srctable,(select max(field) from T1) maxfield union
-> select 'T2' ,(select max(field) from T2) union
-> select 'T3' ,(select max(field) from T3)
-> ) AA
-> );
+-----------+----------+
| srctables | maxfield |
+-----------+----------+
| T1 | 68 |
+-----------+----------+
1 row in set (0.03 sec)
mysql> select max(srctable) srctables,maxfield from
-> (
-> select 'T1' srctable,(select max(field) from T1) maxfield union
-> select 'T2' ,(select max(field) from T2) union
-> select 'T3' ,(select max(field) from T3)
-> ) A WHERE maxfield =
-> (
-> select max(maxfield) from
-> (
-> select 'T1' srctable,(select max(field) from T1) maxfield union
-> select 'T2' ,(select max(field) from T2) union
-> select 'T3' ,(select max(field) from T3)
-> ) AA
-> );
+-----------+----------+
| srctables | maxfield |
+-----------+----------+
| T3 | 68 |
+-----------+----------+
1 row in set (0.00 sec)
mysql> select group_concat(srctable) srctables,maxfield from
-> (
-> select 'T1' srctable,(select max(field) from T1) maxfield union
-> select 'T2' ,(select max(field) from T2) union
-> select 'T3' ,(select max(field) from T3)
-> ) A WHERE maxfield =
-> (
-> select max(maxfield) from
-> (
-> select 'T1' srctable,(select max(field) from T1) maxfield union
-> select 'T2' ,(select max(field) from T2) union
-> select 'T3' ,(select max(field) from T3)
-> ) AA
-> );
+-----------+----------+
| srctables | maxfield |
+-----------+----------+
| T1,T3 | 68 |
+-----------+----------+
1 row in set (0.02 sec)
mysql>
Теперь у вас есть три варианта выбора.
Попробуй !!!