Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts
Astawan News

Format Date di MySQL


Berikut adalah format-format yang dapat digunakan jika kita melakukan query data dari table database yang tipe datanya date.

FORMAT
PENJELASAN
%a
Abbreviated weekday name (Sun..Sat)
%b
Abbreviated month name (Jan..Dec)
%c
Month, numeric (0..12)
%D
Day of the month with English suffix (0th, 1st, 2nd, 3rd, …)
%d
Day of the month, numeric (00..31)
%e
Day of the month, numeric (0..31)
%f
Microseconds (000000..999999)
%H
Hour (00..23)
%h
Hour (01..12)
%I
Hour (01..12)
%i
Minutes, numeric (00..59)
%j
Day of year (001..366)
%k
Hour (0..23)
%l
Hour (1..12)
%M
Month name (January..December)
%m
Month, numeric (00..12)
%p
AM or PM
%r
Time, 12-hour (hh:mm:ss followed by AM or PM)
%S
Seconds (00..59)
%s
Seconds (00..59)
%T
Time, 24-hour (hh:mm:ss)
%U
Week (00..53), where Sunday is the first day of the week; WEEK() mode 0
%u
Week (00..53), where Monday is the first day of the week; WEEK() mode 1
%V
Week (01..53), where Sunday is the first day of the week; WEEK() mode 2; used with %X
%v
Week (01..53), where Monday is the first day of the week; WEEK() mode 3; used with %x
%W
Weekday name (Sunday..Saturday)
%w
Day of the week (0=Sunday..6=Saturday)
%X
Year for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%x
Year for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%Y
Year, numeric, four digits
%y
Year, numeric (two digits)
%%
A literal “%” character
%x
x, for any “x” not listed above
 

Read More
Astawan News

Temporay Table dalam MySQL

Bagi anda yang familiar dengan database MySQL, apakah anda sudah mengenal temporary table? Temporary table adalah tabel sementara yang hanya bisa digunakan oleh satu koneksi dan saat koneksi tersebut diciptakan saja. Maksudnya adalah tabel temporary hanya bisa diakses oleh koneksi yang membuat tabel, tidak seperti tabel lain yang bisa diakses oleh koneksi lain. Jika koneksi tersebut putus, maka tabel temporary juga akan hilang.

Dalam aplikasi database, tabel ini biasanya digunakan untuk dumping data dari beberapa tabel yang dijadikan satu dengan maksud memudahkan aplikasi untuk menampilkan ke pengguna aplikasi. Pembuatannya pun biasanya dilakukan pada saat runtime program.

Berikut adalah sintaks SQL untuk membuat temporary tabel di MySQL:

Create temporary table tempkasir (kodebrg varchar(50), namabrg varchar(100), qty double);

Jika sudah tidak digunakan, tabel tersebut tidak perlu dihapus, karena tabel akan otomatis hilang ketika koneksi terputus. 
Read More
Astawan News

SQL UNION


SQL UNION berfungsi untuk menggabungkan hasil dari beberapa penyaringan data yang menggunakan SELECT.

Sintak dasar:
SELECT field_name(s) FROM table1
UNION
SELECT field_name(s) FROM table2;




atau:

SELECT field_name(s) FROM table1
UNION ALL
SELECT field_name(s) FROM table2;  







Contoh penggunaan:
 Tabel supplier:
kodesup
namasup
S01
TIARA GROSIR
S02
HARAPAN JAYA
S03
SINAR MAS
S04
JAYA SENTOSA
 

Tabel customer:
kodecust
namacust
C01
LAILA
C02
JONI MAHENDRA
C03
TIARA MAHARANI
C04
RANIA MAHARANI
 

Dari kedua tabel diatas, kita ingin menampilkan seluruh data relasi yang kita punya. Data relasi adalah semua orang/perusahaan baik yang memiliki hubungan bisnis dengan kita.
SQL untuk menampilkan data relasi menggunakan UNION: 
SELECT kodesup as kode, namasup as nama FROM supplier
UNION
SELECT kodecust as kode, namacust as nama FROM customer


Hasilnya adalah sebagai berikut:

kode
nama
S01
TIARA GROSIR
S02
HARAPAN JAYA
S03
SINAR MAS
S04
JAYA SENTOSA
C01
LAILA
C02
JONI MAHENDRA
C03
TIARA MAHARANI
C04
RANIA MAHARANI
 
Read More