MySQL Part 1 - Perintah - perintah dasar SQL

Posted by Fajar Ramadhan On Tuesday, November 30, 2010 0 komentar

Silahkan sedot dulu database world disini >>....

MENAMPILKAN SEMUA KOLOM
select * from [namatabel]
perintah * disini maksudnya adalah untuk menampilkan semua data yang ada di tabel..
contohnya :
>> select * from country;
>> select * from city;
>> select * from countrylanguange;

MENAMPILKAN KOLOM TERTENTU
select [namakolom1, namakolom2, dst] from [namatabel]
contohnya :
>> select code, name, headofstate from country;
>> select id,name,countrycode from city;
EXPRESI ARITMETIKA



contohnya :
>> select 3+5 as Jumlah, 5-3 as Selisih, 5*3 as Perkalian, 5/4 as Pembagian, 5 div 3 as pembagian integer, 5mod3 as modulo, 5%3 as sisa pembagian;
ket = as (alias digunakan untuk memberikan nama kolom sesuai selera anda).

>> select name, 2010-indepyear from country;
ket = digunakan untuk menghitung sudah berapa tahun negara tersebut merdeka... (tahun sekarang [2010] dikurangi dengan tahun kemerdekaan [indepyear])

MENGHILANGKAN BARIS YANG SAMA(NAMA BARIS YANG SAMA / BARIS YANG TERDUPLIKAT)
sebelum di duplikat contohnya :
>> select countrycode from city; (menghasilkan 4079 baris)
setelah di duplikat
>> select distinct countrycode from city; (menghasilkan 232 baris)
==============================================================================
MEMBATASI BARIS dengan perintah where
1. where ditulis setelah kata from selesai
2. where biasanya menggunakan operator perbandingan

OPERATOR PERBANDINGAN



Contohnya :
>> pembatasan baris
select code, name, continent
from country
where continent='asia'; {khusus teks diapit tanda kutip tunggal atau ganta}

select name, population
from city
where population>=5000000;

select code, name, continent
from country
where continent<>'asia';

>>select dengan operator in
{seleksi negara asia atau eropa}
select code, name, continent
from country
where continent in('asia','europe');
{seleksi negara bukan asia tau eropa}
select code, name, continent
from country
where continent not in('asia','europe');

>>select dengan operator between
{tujuannya untuk menghasilkan nilai tertentu atau dengan jangka waktu tertentu atau dari data xxx hingga data ke xxx}
select id, name
from city
where population between 5000000 and 6000000;

select id, name
from city
where pupulation between 100 and 200;

>>select dengan operator like
select id, name
from city
where name like 'ban%';

OPERATOR LOGIKA

contohnya :
>> Menampilkan name,population,continent yang ada di tabel country dengan syarat populasinya lebih dari atau sama dengan 500rb dan continentnya harus nort amerika saja...
select name, population, continent
from country
where population>=500000 and continent='North America';

tugas
1. Menampilkan name,population,continent yang ada di tabel country dengan syarat populasinya lebih dari atau sama dengan 500rb atau continentnya harus nort amerika saja...

2. Menampilkan name,population,continent yang ada di tabel country dengan syarat continentnya bukan nort amerika...

                                                                                                                                            Selanjutnya >>

==========================================================================

Sign up for PayPal and start accepting credit card payments instantly.