Mysql
 sql >> डेटाबेस >  >> RDS >> Mysql

MYSQL - डायनामिक टेबल नामों से डेटा चुनें

कोशिश करें:

mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.11    |
+-----------+
1 row in set (0.00 sec)

mysql> DROP TABLE IF EXISTS `ifhcraw_2016_03_25_13`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `ifhcraw_2016_03_26_19`;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS `ifhcraw_2016_03_28_2`;
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `ifhcraw_2016_03_25_13` (
    ->   `id` INT
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `ifhcraw_2016_03_26_19` (
    ->   `id` INT
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE `ifhcraw_2016_03_28_2` (
    ->   `id` INT
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> SET @`TABLE_NAME` := NULL;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT `TABLE_NAME` INTO @`TABLE_NAME`
    -> FROM `INFORMATION_SCHEMA`.`TABLES`
    -> WHERE `TABLE_NAME` LIKE 'ifhcraw%' AND
    ->       `UPDATE_TIME` = (SELECT MAX(`UPDATE_TIME`)
    ->                        FROM `INFORMATION_SCHEMA`.`TABLES`
    ->                        WHERE `TABLE_NAME` LIKE 'ifhcraw%'
    ->                       );
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> SELECT @`TABLE_NAME`;
+---------------+
| @`TABLE_NAME` |
+---------------+
| NULL          |
+---------------+
1 row in set (0.00 sec)

mysql> SET @`qry` := IF(@`TABLE_NAME` IS NULL,
    ->                  'SELECT NULL',
    ->                  CONCAT('SELECT * FROM ', @`TABLE_NAME`));
Query OK, 0 rows affected (0.00 sec)

mysql> PREPARE `stmt` FROM @`qry`;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE `stmt`;
+------+
| NULL |
+------+
| NULL |
+------+
1 row in set (0.00 sec)

mysql> DEALLOCATE PREPARE `stmt`;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO `ifhcraw_2016_03_26_19`
    ->   (`id`)
    -> VALUES
    ->   (1);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT `TABLE_NAME` INTO @`TABLE_NAME`
    -> FROM `INFORMATION_SCHEMA`.`TABLES`
    -> WHERE `TABLE_NAME` LIKE 'ifhcraw%' AND
    ->       `UPDATE_TIME` = (SELECT MAX(`UPDATE_TIME`)
    ->                        FROM `INFORMATION_SCHEMA`.`TABLES`
    ->                        WHERE `TABLE_NAME` LIKE 'ifhcraw%'
    ->                       );
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @`TABLE_NAME`;
+-----------------------+
| @`TABLE_NAME`         |
+-----------------------+
| ifhcraw_2016_03_26_19 |
+-----------------------+
1 row in set (0.00 sec)

mysql> SET @`qry` := IF(@`TABLE_NAME` IS NULL,
    ->                  'SELECT NULL',
    ->                  CONCAT('SELECT * FROM ', @`TABLE_NAME`));
Query OK, 0 rows affected (0.00 sec)

mysql> PREPARE `stmt` FROM @`qry`;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

mysql> EXECUTE `stmt`;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> DEALLOCATE PREPARE `stmt`;
Query OK, 0 rows affected (0.00 sec)

अपडेट करें

जब मापदंड से मेल खाने वाली दो या दो से अधिक तालिकाएँ विफल हो जाएँ तो सावधानी बरती जानी चाहिए:

mysql> INSERT INTO `ifhcraw_2016_03_26_19`
    ->   (`id`)
    -> VALUES
    ->   (1);
Query OK, 1 row affected (0,00 sec)

mysql> INSERT INTO `ifhcraw_2016_03_28_2`
    ->   (`id`)
    -> VALUES
    ->   (1);
Query OK, 1 row affected (0,00 sec)

mysql> SELECT `TABLE_NAME`
    -> FROM `INFORMATION_SCHEMA`.`TABLES`
    -> WHERE `TABLE_NAME` LIKE 'ifhcraw%' AND
    ->       `UPDATE_TIME` = (SELECT MAX(`UPDATE_TIME`)
    ->                        FROM `INFORMATION_SCHEMA`.`TABLES`
    ->                        WHERE `TABLE_NAME` LIKE 'ifhcraw%'
    ->                       );
+-----------------------+
| TABLE_NAME            |
+-----------------------+
| ifhcraw_2016_03_26_19 |
| ifhcraw_2016_03_28_2  |
+-----------------------+
2 rows in set (0,00 sec)

mysql> SELECT `TABLE_NAME` INTO @`TABLE_NAME`
    -> FROM `INFORMATION_SCHEMA`.`TABLES`
    -> WHERE `TABLE_NAME` LIKE 'ifhcraw%' AND
    ->       `UPDATE_TIME` = (SELECT MAX(`UPDATE_TIME`)
    ->                        FROM `INFORMATION_SCHEMA`.`TABLES`
    ->                        WHERE `TABLE_NAME` LIKE 'ifhcraw%'
    ->                       );
ERROR 1172 (42000): Result consisted of more than one row

जैसा आप उचित समझें आपको मामले को संभालना चाहिए।




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. वीबीए - 'mysql सर्वर प्राप्त करना - केवल पढ़ने के लिए' त्रुटि है लेकिन केवल रिकॉर्डसेट का उपयोग करते समय

  2. केवल एक नई पंक्ति डालने पर current_timestamp सेट करें

  3. जहां कुछ समय से अधिक पुराना डेटाटाइम (उदा. 15 मिनट)

  4. रिकॉर्ड का चयन तभी करें जब उसके पहले वाले का मान कम हो

  5. Google Apps स्क्रिप्ट, JDBC और MySQL दिनांक के साथ काम नहीं करता है 0000-00-00