Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CLI MYSQL

ALIAS

Berguna saat ingin mengubah nama kolom atau tabel ketika ingin melakukan select data, akan
berguna saat melakukan join.

• membuat alias pada kolom

-- membuat alias di kolom

select id as kode,

name as nama,

category as kategori,

description as deskripsi,

price as harga,

quantity as jumlah

from products;

• membuat alias untuk tabel

-- membuat alias di tabel

select p.id as kode,

p.name as nama,

p.category as kategori,

p.description as deskripsi,

p.price as harga,

p.qantity as jumlah

from products as p;
WHERE OPERATOR

-- operator perbandingan where clause

select * from products where quantity > 100;

select * from products where quantity <= 50;

select * from products where category != 'makanan';

select * from products where category = 'minuman';

select * from products where quantity > 75 and price > 25000;

select * from products where category = 'makanan' and price < 20000;

select * from products where quantity > 100 or price > 25000;

select * from products where (category = 'makanan' or quantity > 500) and price > 25000;

LIKE OPERATOR

Tidak disarankan digunakan pada database besar

➢ Like ‘b%’ string dengan awalan b


➢ Like ‘%a’ string dengan akhiran a
➢ Like ‘%eko%’ string dengan berisi eko
➢ Not Like tidak like

select * from products where name like '%keju%';

NULL OPERATOR

Operator khusus mencari dan membandingkan

➢ Is null, mencari data null


➢ Is not null, mencari data tidak null

select * from products where description is not null;

BETWEEN
select * from products where price between 15000 and 20000;

select * from products where price not between 15000 and 20000;

IN

Operator untuk melakukan pencarian sebuah kolom dengan beberapa nilai

select * from products where category in ('lain-lain', 'minuman');

select * from products where category not in ('lain-lain', 'minuman');

Order by cluse

Digunakan untuk mengurutkan data berdasarkan kolom yang dipilih, jenis urutan (ASC dan
DESC), bisa juga dilakukan untuk beberapa kolom

LIMIT CLAUSE

select * from products order by id limit 3;

select * from products order by id limit 5;

-- paging dengan mysql

select * from products order by id limit 0, 5; -- 0, skip sedangkan 5 limit (limit pada angka ke-
dua)

select * from products order by id limit 5, 5; -- 5, skip sedangkan 5 limit

select * from products order by id limit 10, 5; -- 0, skip sedangkan 5 limit

select * from products order by id limit 15, 5; -- 0, skip sedangkan 5 limit

SELECT DISTINC DATA

Menghilangkan data duplikan saat melakukan select

• Menghilangkan data duplikat

select distinct category from product;


NUMERIC FUNCTION

Memanipulasi aritmatika

select 10, 10, 10*10 as 'hasil';

select id, name, price, price div 1000 as 'price in k' from products;

AUTO INCREMENT

Digunakan untuk menandai suatu primary key datanya diisi secara otomatis dari angka terakhir
+1, dengan auto increment kita tidak perlu memasukkan data primary ke, ini akan otomatis
dibuat oleh mysql

create table admin(

id int not null auto_increment,

first_name varchar(100) not null,

last_name varchar(100) not null,

primary key (id)

) engine = InnoDB;

insert into admin (first_name, last_name)

values ('eko', 'khennedy'),

('budi', 'nugraha'),

('joko', 'morro');

select * from admin order by id;

delete from admin where id = 3; -- menghapus data pada tabel


• Melihat id terakhir digunakan pada auto increment

select last_insert_id();

STRING FUNCTION

select id,

lower(name) as lower_name,

upper(name) as upper_name,

length(name) as lenght_name

from products;

Lihat di web resmi mysql string function.

DATE TIME FUNCTION

select id, created_at,

extract(year from created_at) as year,

extract(month from created_at) as month

from products;

select id, created_at, year(created_at), month(created_at) from products; -- versi singkat

Lihat di web resmi mysql datetimefunction

You might also like