- 论坛徽章:
- 93
|
夏寥寥 发表于 2016-05-27 17:39 ![]()
目前有2张表A表和B表;B表中的数据是固定不变的;但A表中的数据是可变的,但只可能出现2种情况;
1.A表中 ...
如果可以用动态语句可以试试这个:- mysql> select * from a;
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 1 | 1 |
- | b | 1 | 2 |
- +----+----+----+
- 2 rows in set
- mysql> select * from b;
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 0 | 0 |
- +----+----+----+
- 1 row in set
- mysql> select @count_a := count(*) from a;
- select @str := if (@count_a = 2, 'select * from a', 'select * from b union all select * from a');
- prepare st from @str;
- execute st;
- deallocate prepare st;
- +----------------------+
- | @count_a := count(*) |
- +----------------------+
- | 2 |
- +----------------------+
- 1 row in set
- +-------------------------------------------------------------------------------------------+
- | @str := if (@count_a = 2, 'select * from a', 'select * from b union all select * from a') |
- +-------------------------------------------------------------------------------------------+
- | select * from a |
- +-------------------------------------------------------------------------------------------+
- 1 row in set
- Query OK, 0 rows affected
- Statement prepared
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 1 | 1 |
- | b | 1 | 2 |
- +----+----+----+
- 2 rows in set
- Query OK, 0 rows affected
- mysql>
复制代码
- mysql> select * from a;
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | b | 1 | 2 |
- +----+----+----+
- 1 row in set
- mysql> select * from b;
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 0 | 0 |
- +----+----+----+
- 1 row in set
- mysql> select @count_a := count(*) from a;
- select @str := if (@count_a = 2, 'select * from a', 'select * from b union all select * from a');
- prepare st from @str;
- execute st;
- deallocate prepare st;
- +----------------------+
- | @count_a := count(*) |
- +----------------------+
- | 1 |
- +----------------------+
- 1 row in set
- +-------------------------------------------------------------------------------------------+
- | @str := if (@count_a = 2, 'select * from a', 'select * from b union all select * from a') |
- +-------------------------------------------------------------------------------------------+
- | select * from b union all select * from a |
- +-------------------------------------------------------------------------------------------+
- 1 row in set
- Query OK, 0 rows affected
- Statement prepared
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 0 | 0 |
- | b | 1 | 2 |
- +----+----+----+
- 2 rows in set
- Query OK, 0 rows affected
- mysql>
复制代码 不然,可以用这个试试:
- mysql> select * from a;
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | b | 1 | 2 |
- +----+----+----+
- 1 row in set
- mysql> select * from b;
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 0 | 0 |
- +----+----+----+
- 1 row in set
- mysql> select c1, c2, c3
- from
- (
- select *, 'b' as tb, (select count(*) from a) as cnt from b
- union all
- select *, 'a', (select count(*) from a) from a
- ) as tmp
- where (tb, cnt) in (('b', 1), ('a', 1), ('a', 2));
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 0 | 0 |
- | b | 1 | 2 |
- +----+----+----+
- 2 rows in set
- mysql>
复制代码
- mysql> select * from a;
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 1 | 1 |
- | b | 1 | 2 |
- +----+----+----+
- 2 rows in set
- mysql> select * from b;
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 0 | 0 |
- +----+----+----+
- 1 row in set
- mysql> select c1, c2, c3
- from
- (
- select *, 'b' as tb, (select count(*) from a) as cnt from b
- union all
- select *, 'a', (select count(*) from a) from a
- ) as tmp
- where (tb, cnt) in (('b', 1), ('a', 1), ('a', 2));
- +----+----+----+
- | c1 | c2 | c3 |
- +----+----+----+
- | a | 1 | 1 |
- | b | 1 | 2 |
- +----+----+----+
- 2 rows in set
- mysql>
复制代码 |
|