MYSQL Storage While

Source: Internet
Author: User
Tags prepare stmt

---restore content starts---

A friend in the group, there is a need to get a total of all tables in the database (number of bars)
Idea: Dynamic incoming table name, COUNT (1)

--1. Implement this sentence. Get all table namesCreate TableTEMP_TB (SelectT.table_name,@rownum:=@rownum+1 asNum fromInformation_schema.tables T, (Select @rownum:=0) bwhereT.table_schema='Test' andtable_name not inch('Temp','TEMP_TB'));--2. Get the table structure in the same vein, and put the results of the statistics and the corresponding table names in the table.Create Table Temp(SelectT.table_name,@rownum:=@rownum+1 asNum fromInformation_schema.tables T, (Select @rownum:=0) bwhereT.table_schema='Test' andtable_name not inch('Temp','TEMP_TB'));--3. Delete table Data retention table structureDelete from Temp;--4. Create StorageCreate PROCEDUREWhileloopproc ()BEGINSelect @num:=1,@len:=Count(1) fromTEMP_TB; while @num<@len DoSelect @name:=table_name fromTemp_tbwhereNum=@num;Select @rownum:=Concat'Select COUNT (1)',' as',@name,'Into @temp from',@name); Insert into Temp(Table_name,num)Values(@name,@temp);--Save the executed results to the results tableSet @num:=@num+1;Preparestmt from @rownum; EXECUTEstmt;deallocate PREPAREstmt;End while;End;--5. Execute the storageCall Whileloopproc;--6. Query ResultsSelect * from Temp;

Done!


MYSQL Storage While

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.