Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts
Fungsi INSTR di MySQL
mysql> select * from aa;
+---------+---------+
| id | angka |
+---------+---------+
| 1 | 2,3,5,4 || 4 | 4 |
| 2 | 2 |
| 3 | 2,6 |
+---------+---------+
4 rows in set (0.00 sec)
mysql> select * from aa where INSTR(CONCAT(',', angka,','), ',2,');
+---------+---------+
| id | angka |
+---------+---------+
| 1 | 2,3,5,4 |
| 2 | 2 |
| 3 | 2,6 |
+---------+---------+
3 rows in set (0.00 sec)
mysql> select * from aa where INSTR( angka, '4');
+---------+---------+
| id | angka |
+---------+---------+
| 1 | 2,3,5,4 |
| 4 | 4 |
+---------+---------+
2 rows in set (0.00 sec)
Contoh Query Select For Update SQL
function cek($key){
$this->db->query("start transaction");
return $this->db->query("SELECT jumlah from mytable where kode='$key' for update")->row();
}
...
$this->db->query("start transaction");
return $this->db->query("SELECT jumlah from mytable where kode='$key' for update")->row();
}
...
Sub Query Sederhana untuk membuat Laporan MySQL
mysql> select * from pegawai;
+------+--------+
| nik | nama |
+------+--------+
| PEG1 | Pampam |
| PEG2 | Agnes |
| PEG3 | Catur |
+------+--------+
mysql> select * from produksi;
+-------------+------+---------+--------+
| id_produksi | nik | barang | jumlah |
+-------------+------+---------+--------+
| 1 | PEG1 | barang1 | 4 |
| 2 | PEG1 | barang1 | 7 |
| 3 | PEG1 | barang1 | 1 |
| 4 | PEG2 | barang1 | 3 |
| 5 | PEG2 | barang1 | 1 |
| 6 | PEG3 | barang1 | 9 |
| 7 | PEG3 | barang2 | 9 |
| 8 | PEG2 | barang2 | 1 |
| 9 | PEG1 | barang2 | 5 |
+-------------+------+---------+--------+
9 rows in set (0.00 sec)
mysql> select nik as nik_pegawai,nama,(select sum(jumlah) from produksi where nik=nik_pegawai) as jumlah_produksi from produksi inner join pegawai using (nik) group by nik;
+-------------+--------+-----------------+
| nik_pegawai | nama | jumlah_produksi |
+-------------+--------+-----------------+
| PEG1 | Pampam | 17 |
| PEG2 | Agnes | 5 |
| PEG3 | Catur | 18 |
+-------------+--------+-----------------+
3 rows in set (0.01 sec)
mysql> select nik as nik_pegawai,nama,barang as barang_produksi,(select sum(jumlah) from produksi where nik=nik_pegawai and barang=barang_produksi) as jumlah_produksi from produksi inner join pegawai using (nik) group by nik,barang;
+-------------+--------+-----------------+-----------------+
| nik_pegawai | nama | barang_produksi | jumlah_produksi |
+-------------+--------+-----------------+-----------------+
| PEG1 | Pampam | barang1 | 12 |
| PEG1 | Pampam | barang2 | 5 |
| PEG2 | Agnes | barang1 | 4 |
| PEG2 | Agnes | barang2 | 1 |
| PEG3 | Catur | barang1 | 9 |
| PEG3 | Catur | barang2 | 9 |
+-------------+--------+-----------------+-----------------+
6 rows in set (0.00 sec)
<?php
$con=mysql_connect('localhost','abby','abby');
if(!mysql_select_db('abby',$con))
{die("database salah");}
$nik='xxx';
echo"<table align='center' border='1'>
<tr>
<th>NIK</th>
<th>Nama Pegawai</th>
<th>Nama Barang</th>
<th>Jumlah Produksi</th>
</tr>";
$q=mysql_query("select nik as nik_pegawai,nama,barang as barang_produksi,(select sum(jumlah) from produksi where nik=nik_pegawai and barang=barang_produksi) as jumlah_produksi from produksi inner join pegawai using (nik) group by nik,barang");
while($d=mysql_fetch_array($q))
{
if($nik!=$d[nik_pegawai])
{
$nik=$d['nik_pegawai'];
$nama=$d['nama'];
}
else
{
$nik='';
$nama='';
}
echo "<tr>
<td>$nik</td>
<td>$nama</td>
<td>$d[barang_produksi]</td>
<td>$d[jumlah_produksi]</td>
</tr>";
$nik=$d['nik_pegawai'];
$nama=$d['nama'];
}
echo"</table>";
?>
+------+--------+
| nik | nama |
+------+--------+
| PEG1 | Pampam |
| PEG2 | Agnes |
| PEG3 | Catur |
+------+--------+
mysql> select * from produksi;
+-------------+------+---------+--------+
| id_produksi | nik | barang | jumlah |
+-------------+------+---------+--------+
| 1 | PEG1 | barang1 | 4 |
| 2 | PEG1 | barang1 | 7 |
| 3 | PEG1 | barang1 | 1 |
| 4 | PEG2 | barang1 | 3 |
| 5 | PEG2 | barang1 | 1 |
| 6 | PEG3 | barang1 | 9 |
| 7 | PEG3 | barang2 | 9 |
| 8 | PEG2 | barang2 | 1 |
| 9 | PEG1 | barang2 | 5 |
+-------------+------+---------+--------+
9 rows in set (0.00 sec)
Query berdasarkan nik pegawai:
mysql> select nik as nik_pegawai,nama,(select sum(jumlah) from produksi where nik=nik_pegawai) as jumlah_produksi from produksi inner join pegawai using (nik) group by nik;
+-------------+--------+-----------------+
| nik_pegawai | nama | jumlah_produksi |
+-------------+--------+-----------------+
| PEG1 | Pampam | 17 |
| PEG2 | Agnes | 5 |
| PEG3 | Catur | 18 |
+-------------+--------+-----------------+
3 rows in set (0.01 sec)
Query berdasarkan nik dan barang:
mysql> select nik as nik_pegawai,nama,barang as barang_produksi,(select sum(jumlah) from produksi where nik=nik_pegawai and barang=barang_produksi) as jumlah_produksi from produksi inner join pegawai using (nik) group by nik,barang;
+-------------+--------+-----------------+-----------------+
| nik_pegawai | nama | barang_produksi | jumlah_produksi |
+-------------+--------+-----------------+-----------------+
| PEG1 | Pampam | barang1 | 12 |
| PEG1 | Pampam | barang2 | 5 |
| PEG2 | Agnes | barang1 | 4 |
| PEG2 | Agnes | barang2 | 1 |
| PEG3 | Catur | barang1 | 9 |
| PEG3 | Catur | barang2 | 9 |
+-------------+--------+-----------------+-----------------+
6 rows in set (0.00 sec)
Tampilkan dalam bentuk file PHP :
<?php
$con=mysql_connect('localhost','abby','abby');
if(!mysql_select_db('abby',$con))
{die("database salah");}
$nik='xxx';
echo"<table align='center' border='1'>
<tr>
<th>NIK</th>
<th>Nama Pegawai</th>
<th>Nama Barang</th>
<th>Jumlah Produksi</th>
</tr>";
$q=mysql_query("select nik as nik_pegawai,nama,barang as barang_produksi,(select sum(jumlah) from produksi where nik=nik_pegawai and barang=barang_produksi) as jumlah_produksi from produksi inner join pegawai using (nik) group by nik,barang");
while($d=mysql_fetch_array($q))
{
if($nik!=$d[nik_pegawai])
{
$nik=$d['nik_pegawai'];
$nama=$d['nama'];
}
else
{
$nik='';
$nama='';
}
echo "<tr>
<td>$nik</td>
<td>$nama</td>
<td>$d[barang_produksi]</td>
<td>$d[jumlah_produksi]</td>
</tr>";
$nik=$d['nik_pegawai'];
$nama=$d['nama'];
}
echo"</table>";
?>
Hasil :
Membuat User di MySQL [Debian]
Berhubung lagi ngeremote, dan masih berhubungan dengan DBMS MySQL, sekedar membuat catatan bagaimana membuat user di DBMS MySQL :
1. Masuk MySQL dengan user root :
# mysql -u root -p
2. Buat Hash Password dengan perintah berikut :
contoh : abby
> select password ('abby');
hasilnya seperti berikut :
3. Buat User dengan perintah berikut dan password dengan hasil hash tadi :
> create user 'abby'@'localhost' identified by password '*8DF79545DB6D735C372251BA785BF66DCB982705';
4. Lihat hasilnya di dalam tabel user database mysql :
> select user, password from mysql.user where user='abby';
5. Sekarang coba buat Database untuk User yang sudah dibuat :
> create database db_abby;
6. Beri Hak Akses User untuk database yang sudah dibuat :
> grant all on db_abby.* to 'abby'@'localhost';
7. Keluar dan Masuk dengan user yang sudah dibuat :
Semoga Membantu..
Subscribe to:
Posts (Atom)