mysql打印特定表范围内的数据

时间:2017-02-21 08:34:38

标签: mysql

我有mySQL数据库名称' conntrack'以下表格

root@Radius:/temp# mysql -uroot -pMYSQLPASS "use conntrack; show tables;"
+------------+
| 2016-11-24 |
| 2016-11-25 |
| 2016-11-26 |
| 2016-11-27 |
| 2016-11-28 |
| 2016-11-29 |
| 2016-11-30 |
+------------+

表具有以下结构,(示例)

mysql> describe `2016-12-25`;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| time     | time        | NO   | MUL | NULL    |       |
| username | varchar(32) | NO   | MUL | NULL    |       |
| srcip    | varchar(15) | NO   | MUL | NULL    |       |
| srcport  | varchar(5)  | NO   | MUL | NULL    |       |
| dstip    | varchar(15) | NO   | MUL | NULL    |       |
| dstport  | varchar(5)  | NO   | MUL | NULL    |       |
| protocol | char(1)     | NO   |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+

我希望从2016-11-24到2016-11-26的表格范围内获取dstport字段。如何使用单线程命令?

1 个答案:

答案 0 :(得分:1)

试试这个:

SELECT COLUMN_NAME, COLUMN_TYPE, IS_NULLABLE, COLUMN_KEY, COLUMN_DEFAULT, EXTRA
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME BETWEEN '2016-11-24' AND '2016-11-26'

<强> 被修改

SET @S = NULL;
SELECT GROUP_CONCAT(CONCAT('SELECT DSTPORT FROM `', TABLE_NAME, '`') SEPARATOR ' UNION ') INTO @S
FROM (
    SELECT DISTINCT TABLE_NAME, 1 AS GRP
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME BETWEEN '2016-11-24' AND '2016-11-26'
) T
GROUP BY GRP;
PREPARE STMT FROM @S;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;

不确定这是否是您想要的,但请尝试一下。