gpt4 book ai didi

mysql select查询多个表名字符串

转载 作者:行者123 更新时间:2023-11-29 03:31:14 25 4
gpt4 key购买 nike

我想在 mysql 中执行以下查询。我怎样才能在 mysql 中的单个选择查询中做到这一点?我想在表列表中运行相同的计数语句。内部查询返回动态表名字符串。

SELECT COUNT(*)  FROM ( list of table names from a select statemt)   Where Col1 is NULL;

SELECT COUNT(*) FROM ( SELECT CONCAT('TABLE',id) TABLENAMES FROM CUSTOMTABLE WHERE Active=1) AA WHERE AA.COL1 IS NULL;

谁能帮我解决这个问题?

最佳答案

你需要的是两件事的结合:

  1. 集合union运算符,和
  2. 返回涉及联合的中间操作结果的子查询。

假设您有一个account 表和一个employee 表。

mysql> desc account;+--------------------+----------------------------------+------+-----+---------+----------------+| Field              | Type                             | Null | Key | Default | Extra          |+--------------------+----------------------------------+------+-----+---------+----------------+| account_id         | int(10) unsigned                 | NO   | PRI | NULL    | auto_increment || product_cd         | varchar(10)                      | NO   | MUL | NULL    |                || cust_id            | int(10) unsigned                 | NO   | MUL | NULL    |                || open_date          | date                             | NO   |     | NULL    |                || close_date         | date                             | YES  |     | NULL    |                || last_activity_date | date                             | YES  |     | NULL    |                || status             | enum('ACTIVE','CLOSED','FROZEN') | YES  |     | NULL    |                || open_branch_id     | smallint(5) unsigned             | YES  | MUL | NULL    |                || open_emp_id        | smallint(5) unsigned             | YES  | MUL | NULL    |                || avail_balance      | float(10,2)                      | YES  |     | NULL    |                || pending_balance    | float(10,2)                      | YES  |     | NULL    |                |+--------------------+----------------------------------+------+-----+---------+----------------+11 rows in set (0.00 sec)mysql> desc employee;+--------------------+----------------------+------+-----+---------+----------------+| Field              | Type                 | Null | Key | Default | Extra          |+--------------------+----------------------+------+-----+---------+----------------+| emp_id             | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment || fname              | varchar(20)          | NO   |     | NULL    |                || lname              | varchar(20)          | NO   |     | NULL    |                || start_date         | date                 | NO   |     | NULL    |                || end_date           | date                 | YES  |     | NULL    |                || superior_emp_id    | smallint(5) unsigned | YES  | MUL | NULL    |                || dept_id            | smallint(5) unsigned | YES  | MUL | NULL    |                || title              | varchar(20)          | YES  |     | NULL    |                || assigned_branch_id | smallint(5) unsigned | YES  | MUL | NULL    |                |+--------------------+----------------------+------+-----+---------+----------------+9 rows in set (0.00 sec)

Now, you want to count the total number of rows in both the tables satisfying certain conditions. So, you use union first:

select account_id id from account union all select emp_id id from employee

This gives you a table that represents a union of these two tables account and employee and you make sure that they have the same 'data' that agrees with union operation. The above query is the simplest, it just selects emp_id from employee table and account_id from account table and renames the columns to id. This makes the data 'union-friendly'. Of course, you can apply your filter conditions here. The all operator selects all the records even if there are duplicates (Remember from set theory, a set contains all unique members).

Now, the first part, the subquery:

mysql> select count(*) from 
(select account_id id from account union all
select emp_id id from employee) u;

然后返回:

+----------+
| count(*) |
+----------+
| 42 |
+----------+
1 row in set (0.00 sec)

并且可以验证:

mysql> select count(*) from employee;
+----------+
| count(*) |
+----------+
| 18 |
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from account;
+----------+
| count(*) |
+----------+
| 24 |
+----------+
1 row in set (0.00 sec)

关于mysql select查询多个表名字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30293963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com