获取不同表和它所来自的表之间的最大字段值

时间:2012-03-22 17:47:24

标签: mysql sql database

我有很多桌子,比如T1,T2,T3。

每个表都有一个名为field的列。我想在三个表中得到field的最大值,但也来自哪个表,例如

T1 { 6, 8, 23 }
T2 { 8, 45, 23 }
T3 { 68, 5, 67 }

我必须知道它属于表T3。 我使用MySQL作为DBMS。 可能吗?提前谢谢。

3 个答案:

答案 0 :(得分:3)

也许有一个我不知道的问题,但以下似乎是一个可能的解决方案:

SELECT 'T1' AS Source, field FROM T1
UNION ALL
SELECT 'T2' AS Source, field FROM T2
UNION ALL
SELECT 'T3' AS Source, field FROM T3

ORDER BY field DESC
LIMIT 1

答案 1 :(得分:2)

这是查询

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 EDT

我刚刚意识到了什么。如果多个表具有相同的最大值会怎样?

您可以使用三种功能中的一种来纠正您想要查看的表:

  • MIN(第一个匹配表)
  • MAX(最后匹配表)
  • GROUP_CONCAT(所有匹配表)

我将重新加载您的样本数据,并在T1中添加68

    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>

现在您有三种解决方案可供选择。

试一试!!!

答案 2 :(得分:0)

您可以尝试以下查询..

with tab1 as
(select 't1' table_name, max(field) from t1
union all 
select 't2' table_name, max(field) from t2
union all 
select 't3' table_name, max(field) from t3
union all 
select 't4' table_name, max(field) from t4
.
.
.
.
.
) 
select * from 
tab1 tab2
where tab2.field = (select max(tab3.field) from tab1 as tab3);

我希望此查询可以解决您的问题。