各位大牛,請幫忙看一下如下SQL,第1個Union ALL去掉,然后自動執行concat出來的結果,怎么實作?貼出存盤程序的腳本,謝謝!

uj5u.com熱心網友回復:
需要寫2個陳述句,順序執行即可。第一句是定義你的陳述句,第二句是執行。樣例代碼如下:PREPARE stmt1 FROM 'select t.* from table_name t';
EXECUTE stmt1 ;
如果需要傳入引數,樣例如下,問號是變數的位置,定義的變數和問號的數量要相等。按照先后順序,問號和變數要對應。樣例代碼如下:
PREPARE stmt1 FROM 'select t.* from table_name t where t.id = ? and t.name = ?';
SET @a = 3;
SET @b = 4;
EXECUTE stmt1 USING @a, @b;
uj5u.com熱心網友回復:
HI,AHUA1001
SELECT CONCAT('union all select ''', TABLE_SCHEMA ,''' as db ,''',TABLE_NAME,''' as tbname, count(1) as ro ', TABLE_SCHEMA ,'.',TABLE_NAME ) AS SqlExe FROM information_schema.TABLES AS t WHERE t.TABLE_TYPE = 'BASE TABLE' AND t.TABLE_SCHEMA = 'test'
這是SQL陳述句,查出來的結果是
+---------------------------------------------------------------------------------------+
| SqlExe |
+---------------------------------------------------------------------------------------+
| union all select 'test' as db ,'accesslog' as tbname, count(1) as ro test.accesslog |
| union all select 'test' as db ,'t' as tbname, count(1) as ro test.t |
| union all select 'test' as db ,'t1' as tbname, count(1) as ro test.t1 |
| union all select 'test' as db ,'t2' as tbname, count(1) as ro test.t2 |
| union all select 'test' as db ,'temp' as tbname, count(1) as ro test.temp |
| union all select 'test' as db ,'tn' as tbname, count(1) as ro test.tn |
| union all select 'test' as db ,'tt' as tbname, count(1) as ro test.tt |
+---------------------------------------------------------------------------------------+
7 rows in set (0.00 sec)
現在需求是,將查出來的結果(如上)第1個union all去掉,單獨執行后面的內容,如下,請幫忙,謝謝!
select 'test' as db ,'accesslog' as tbname, count(1) as ro test.accesslog
union all select 'test' as db ,'t' as tbname, count(1) as ro test.t
union all select 'test' as db ,'t1' as tbname, count(1) as ro test.t1
union all select 'test' as db ,'t2' as tbname, count(1) as ro test.t2
union all select 'test' as db ,'temp' as tbname, count(1) as ro test.temp
union all select 'test' as db ,'tn' as tbname, count(1) as ro test.tn
union all select 'test' as db ,'tt' as tbname, count(1) as ro test.tt
uj5u.com熱心網友回復:
這個簡單,您要的結果是一大堆的union all對吧。就是一個select查詢出來的。
以下,"null,null……"里的null數量,要和您的那個大查詢陳述句里的列數保持一致。
select null,null…… from dual where 1=2 union all
這里寫你的那一大堆陳述句。
思路和您相反,您是想去掉最前的union all,我是在最前的union all加上一個東西,并且加的這個東西where 1=2,不會顯示。
另外感覺您的這個回傳結果,有問題,可能執行不過去,你除錯一下吧,有問題再回復。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/62390.html
標籤:MySQL
