2011-12-06 15 views

Odpowiedz

11
select sum(table_rows) as total_rows 
from information_schema.tables 
where table_schema = 'your_db_name' 

uwaga jest to tylko wartość przybliżona

Aby usunąć zawartość wszystkich tabel można zrobić coś takiego to

select concat('truncate ',table_name,';') 
from information_schema.tables 
where table_schema = 'your_db_name' 

Następnie uruchom wyjście tego zapytania.

AKTUALIZACJA.

Jest to procedura przechowywana zastosować truncate table do wszystkich tabel w określonej bazie

delimiter // 
drop procedure if exists delete_contents // 
create procedure delete_contents (in db_name varchar(100)) 
begin 
declare finish int default 0; 
declare tab varchar(100); 
declare cur_tables cursor for select table_name from information_schema.tables where table_schema = db_name and table_type = 'base table'; 
declare continue handler for not found set finish = 1; 
open cur_tables; 
my_loop:loop 
fetch cur_tables into tab; 
if finish = 1 then 
leave my_loop; 
end if; 

set @str = concat('truncate ', tab); 
prepare stmt from @str; 
execute stmt; 
deallocate prepare stmt; 
end loop; 
close cur_tables; 
end; // 
delimiter ; 

call delete_contents('your_db_name'); 
+1

ah .. dzięki. Przygotuj stst z @str jest bardzo przydatny. – scravy

+0

Twoja zaktualizowana procedura jest bardzo przydatna. Wygląda na to, że 'db_name' nie ma wpływu na nic, ale z bieżącą konfiguracją (usunąłem ją całkowicie bez problemu w procedurze). – DACrosby

+0

Nice. Aby wyświetlić indywidualną liczbę wpisów w każdej tabeli: select table_name,table_rows from information_schema.tables where table_schema = 'your_db_name' gaoithe

0

jeśli tabele są powiązane każdej dziedzinie można wykorzystać alias tabel jak

select count(*) from table1 tb1, table2 tb2, table3 tb3 where 
tb1.field1 = tb2.field2 and tb2.field2 = tb3.field3 

similary,

delete from table1 tb1, table2 tb2, table3 tb3 where 
tb1.field1 = tb2.field2 and tb2.field2 = tb3.field3 

można uwzględnić warunki zgodnie z wymaganiami.

Jeśli tabele mają żadnego związku następnie skorzystać z poniższego

SELECT 
    (SELECT COUNT(*) FROM table1 WHERE someCondition) as count1, 
    (SELECT COUNT(*) FROM table2 WHERE someCondition) as count2, 
    (SELECT COUNT(*) FROM table3 WHERE someCondition) as count3 

można usunąć, gdy klauzula jeśli nie ma warunków.

WYJŚCIE:

| count1 | count2 | count3 |

| 50 | 36 | 21 |

Powiązane problemy