Postingan lainnya
Penjumlahan Tabel di Mysql
hallo bro..
saya mau menjumlahkan isi seluruh baris antar tabel, misalkan : tabel1 + tabel2 = hasil dari penjumlahan masing" tabel bagaimana coding sql, untuk menjumlahkan isi seluruh tabel di database ?
10 Jawaban:
kurang jelas pertanyaannya. jumlah dari masing-masing isi table kah? misal jumlah row table1 = 50 jumlah row table2 = 150
atau apa gan?
iya bro, jadi seperti ini, jumlah row table1 = 50 jumlah row table2 = 150
jadi, tabel1+tabel2=200
untuk coding nya gimana ya bro ?
Terima Kasih
simple nya <pre> SELECT (SELECT count() FROM table1)+(SELECT count() FROM table2) as jumlah </pre>
masih belum tampil gan hasil nya.
ini coding ane : <?php $query=mysql_query("SELECT (SELECT count() FROM archive_laptop)+(SELECT count() FROM archive_komputer)"); ?> <?php echo mysql_num_rows($query); ?>
coba <pre> <?php $query=mysql_query("SELECT (SELECT count() FROM archive_laptop)+(SELECT count() FROM archive_komputer) as Jumlah"); ?> <?php echo mysql_num_rows($query); ?> </pre>
ini isi row table nya gan,
TABLE archive_komputer
(
id
user_id
no_inventaris
pengguna`
archive_komputer (
id,
user_id,
no_inventaris,
pengguna`) VALUES
(1, 'NIK_0001', '1', '1');
TABLE archive_laptop
(
id
user_id
no_inventaris
pengguna
archive_laptop (
id,
user_id,
no_inventaris,
pengguna`) VALUES
(1, 'NIK_0002', '2', '2');
jadi row dari masing-masing table mau ane jumlahin gan.
kalo script berikut, hanya menampilkan jumlah table, bukan jumlah row dari masing-masing table :
<?php $query=mysql_query("SELECT COUNT() FROM archive_laptop UNION ALL SELECT COUNT() FROM archive_komputer"); ?> <?php echo mysql_num_rows($query); ?>
Klw kodingannya @Nandar bagian ini: <pre><?php echo mysql_num_rows($query); ?> </pre> Jadi : <pre><?php echo mysql_fetch_object($query)->Jumlah; ?></pre>
@Nandar dan @devio , terima kasih banyak atas jawaban kalian.