our notes for our youngers

October 2, 2009

basic of C# (part 25)

Filed under: Basic of C# — Tags: , , , , , — sevenlamp @ 10:21 AM

Function Overloading Vs Function Overriding

ကြၽန္ေတာ္တို႔ ဒီေန႔ function overloading နဲ႔ function overriding အေၾကာင္းေလး ေျပာၾကရေအာင္။ အရင္ဆံုး function overloading အေၾကာင္းေျပာရေအာင္။ overload ဆိုတာ အလုပ္တစ္ခုထက္ပိုျပီး လုပ္တာကို ေျပာခ်င္တာပါ။ function overloading ဆိုေတာ့ ကြၽန္ေတာ္တို႔ ေရးလိုက္တဲ႔ function က တာ၀န္ တစ္ခုထက္ပိုျပီး လုပ္ေဆာင္ေပးတာကိုရည္ရြယ္တာပါ။ function name က တစ္ခုထဲပဲ ဒါေပမယ့္ လုပ္ေဆာင္မႈအမ်ားၾကီး လုပ္နိုင္ေအာင္ေပါ့။ ဥပမာဗ်ာ.. ကြၽန္ေတာ္က string ကေန integer ေျပာင္းတဲ႔ function ေလးတစ္ခုေလာက္ ေရးခ်င္တယ္။

static int ConvertToInteger(string st)
{
    try
    {
        return Convert.ToInt32(st);
    }
    catch
    {
        return 0;
    }
}

ကဲ.. ဒီ function ေလး ေရးလိုက္ျခင္းအားျဖင့္ ကြၽန္ေတာ္တို႔ string ကေန integer ေျပာင္းခ်င္တိုင္း လြယ္လြယ္ကူကူ ေခၚလို႔ ရသြားမယ္။ ဒါေပမယ့္ တျခား type ေတြကေနေျပာင္းခ်င္ရင္ ဒီ function သံုးလို႔ ရမလား။ မရပါဘူး။ ဒါဆို ကြၽန္ေတာ္က တျခားtype ေတြ အတြက္ကိုလည္း ဒီလိုမ်ိဳး function ေတြ ထပ္ေရးေပးဖို႔ လိုပါလိမ့္မယ္။

static int ConvertToInteger(decimal dec)
{
    try
    {
        return Convert.ToInt32(dec);
    }
    catch
    {
        return 0;
    }
}
static int ConvertToInteger(long l)
{
    try
    {
        return Convert.ToInt32(l);
    }
    catch
    {
        return 0;
    }
}

ဟုတ္ျပီေနာ္.. အခုလိုမ်ိဳး function name တူျပီး parameter မတူတဲ႔ function ေတြ တစ္ခုထက္ပိုျပီး ေရးလိုက္တာကို function overloading လုပ္တယ္လို႔ ေခၚပါတယ္။ function overloading လုပ္ျခင္း အားျဖင့္ ဘာပိုေကာင္းလာမလဲ။ ဟုတ္ကဲ႔ ကြၽန္ေတာ္တို႔ program ေရးတဲ႔ေနရာမွာ အမ်ားၾကီးပိုျပီး လြယ္ကူေစပါတယ္။ အကယ္လို႔သာ function overload မလုပ္ရဘူးဆိုရင္ ကြၽန္ေတာ္တို႔ string ကေန integer ေျပာင္းဖို႔အတြက္က function name တစ္ခု၊ decimal ကေန integer ေျပာင္းဖို႔အတြက္က function name တစ္ခု၊ long ကေန integer ေျပာင္းဖို႔အတြက္က function name တစ္ခု စသည္ျဖင့္ name ေတြ အမ်ားၾကီး သံုးေနမွတ္ေနရမွာေပါ့။ အခု overloading လုပ္ျခင္းအားျဖင့္ function name တစ္ခုထဲကိုပဲ သိဖို႔ လိုပါေတာ့တယ္။ Framework class library ထဲမွာလည္း overload လုပ္ထားတဲ႔ function ေတြ အမ်ားၾကီး ေတြရမွာပါ။ ကဲ အႏွစ္ခ်ဳပ္ျပန္ေျပာပါမယ္ function overloading ျဖစ္ဖို႔အတြက္ function name တူရပါမယ္၊ argument (parameter) မတူရပါဘူး။ parameter မတူရဘူး ဆိုတာမွာ parameter ရဲ႕ datatype ျဖစ္ေစ၊ အရည္အတြက္ ျဖစ္ေစမတူရင္ ရပါတယ္။

ေနာက္တစ္ခုကေတာ့ function overriding။ overriding လို႔ ေျပာတဲ႔ အတိုင္းပါပဲ၊ function overrideing ဆိုတာက function တစ္ခုေပၚမွာ ေနာက္ထပ္ function တစ္ခုနဲ႔ ထပ္ျပီး အုပ္လိုက္တာကို ေျပာခ်င္တာပါ။ ဒီလိုလုပ္လိုက္ရင္ function ၂ ခု ျဖစ္သြားမလား၊ မျဖစ္ပါဘူး။ override လုပ္ျခင္းခံရတဲ႔ function က ေပ်ာက္သြားမွာပါ။ function overrideing ကို အဓိကအားျဖင့္ polymorphism နဲ႔ တြဲျပီးသံုးေလ့ ရွိပါတယ္။ parent class ထဲမွာ ရွိျပီးသား function တစ္ခုကို child class ထဲမွာျပန္ျပီးေရးခ်င္တယ္ ဆိုရင္ override လုပ္ဖို႔ လိုလာပါျပီ။ ဒီလို အခ်ိန္မ်ိဳးမွာ function overriding ကိုသံုးပါတယ္။ ဥပမာေလးနဲ႔ ေျပာရရင္… ကြၽန္ေတာ္မွာ animal ဆိုတဲ႔ class ရွိတယ္ဗ်ာ၊ parent class ေပါ့၊ အဲ့ဒီ animal class ထဲမွာ eat() ဆိုတဲ႔ function ေလးပါတယ္။

class animal
{
    public void eat()
    {
        Console.WriteLine("Eat something");
    }
}

အိုေက.. အဲ့ဒီ animal class မွာ child class ၂ ခုရွိတယ္။ dog class နဲ႔ ant class ေပါ့။ အဲ့ဒီ dog class နဲ႔ ant class က animal class ကေန inheritance လုပ္ထားတာျဖစ္တဲ႔ အတြက္ သူတို႔ဆီမွာ eat() function အလိုအေလွ်ာက္ ပါလာပါတယ္။ အဲ့ဒီ parent class ကေနပါလာတဲ႔ eat() function ထဲက statement ေတြကို child class မွာ မလိုခ်င္ဘူး။ ပိုျပီး detail ၾကတဲ႔ message နဲ႔ အစားထိုးခ်င္တယ္ဆိုပါေတာ့ (ဥပမာ.. Eat something အစား၊ Eat Bone တို႔ Eat sugur တို႔နဲ႔ အစားထိုးခ်င္ျပီဆိုရင္)၊ ဒါဆို ဘယ္လိုလုပ္မလဲ? ေနာက္ထပ္ function တစ္ခုထပ္ေဆာက္ခ်င္တာ မဟုတ္ဘူးေနာ္၊ parameter လည္း မေျပာင္းတဲ႔ အတြက္ overloading လည္း လုပ္လို႔ မရဘူး။ ဒီလို အေျခအေနမ်ိဳးမွာ ကြၽန္ေတာ္တို႔ မျဖစ္မေန function overriding လုပ္ဖို႔ လိုလာပါျပီ။

ဒါဆို function overriding လုပ္ဖို႔ ဘာလိုမလဲ? Parent class ထဲမွာ ဒီတိုင္းေရးထားတဲ႔ function တိုင္းကိုေတာ့ override လုပ္လို႔ မရပါဘူး။ override လုပ္ဖို႔ ခြင့္ျပဳထားတဲ႔ function ေတြကိုပဲ လုပ္လို႔ရတာပါ။ ဒီလို override လုပ္ဖို႔ ခြင့္ျပဳခ်င္တယ္ဆိုရင္ parent class ထဲက function ရဲ႕ေရွ႕မွာ virtual ဆိုတဲ႔ keyword ေလးထည့္ေရးေပးလိုက္ရပါတယ္။

class animal
{
    public virtual void eat()
    {
        Console.WriteLine("Eat something");
    }
}

override ျပန္လုပ္မယ့္ child class က function ေရွ႕မွာေတာ့ override ဆိုတဲ႔ keywordကို ထည့္ေပးရပါတယ္။

class dog : animal
{
    public override void eat()
    {
        Console.WriteLine("Eat Bone!");
    }
}

ကဲ… example program ေလး တစ္ခုေလာက္ေရးၾကည့္ပါဦး။

class animal
{
    public virtual void eat()
    {
        Console.WriteLine("Animal: Eat something");
    }
}
class dog : animal
{
    public override void eat() // function overriding
    {
        Console.WriteLine("Dog: Eat Bone!");
    }
}
class ant : animal
{
    public override void eat()
    {
        Console.WriteLine("Ant: Eat Sugar");
    }
}
class Program
{
    static void Main()
    {
        animal obj;
        obj = new animal();
        obj.eat();

        obj = new dog(); //polymorphism
        obj.eat();

        obj = new ant();
        obj.eat();

        Console.Read();
    }
}

Basic of C# ကို ကြၽန္ေတာ္ဒီမွာပဲ ရပ္လိုက္ပါေတာ့မယ္။ အခုေလာက္ဆိုရင္ ကြၽန္ေတာ္တို႔ C# ဆိုတာ ဘာလဲသိေလာက္ပါျပီ။ OOP အေၾကာင္းလည္း နည္းနည္းေတာ့ နားလည္ေလာက္ျပီလို႔ ယူဆပါတယ္။ ေနာက္ပိုဒ႔္ေတြမွာ window application အပိုင္းနဲ႔ web application အပိုင္းကို ဆက္ေရးသြားပါ့မယ္ခင္ဗ်ာ။

September 18, 2009

basic of SQL Statement (part 6)

Filed under: SQL Commands — Tags: , , , , , , — sevenlamp @ 10:00 AM

Sql statement ေတြဘက္ကိုေတာ့ မလွည့္ျဖစ္တာ ေတာ္ေတာ္ၾကာသြားျပီ။ ကြၽန္ေတာ္လည္း ေရးခ်င္တာေတြကေတာ့ အမ်ားၾကီး အခ်ိန္ကမရွိဘူးျဖစ္ေနတာနဲ႔ ေရွ႕မဆက္နိုင္ဘူးျဖစ္ေနတာ။ ဘာဘဲျဖစ္ျဖစ္ ရတဲ႔အခ်ိန္ေလးလုျပီး ကြၽန္ေတာ္သိသေလာက္ေတာ့ ထပ္ေရးေနဦးမွာမို႔ အားေပးၾကပါဦးဗ်ာ။ ကဲ… ကြၽန္ေတာ္တို႔ select statement ကို ေျပာလက္စ ဆက္ပါဦးမယ္။ table တစ္ခုထဲက data ကို ထုတ္ၾကည့္တာကိုေတာ့ နားလည္ေလာက္ျပီထင္ပါတယ္။ အခု ကြၽန္ေတာ္တို႔ တစ္ခုထက္ပိုတဲ႔ table ေတြထဲက data ေတြကို ထုတ္ၾကည့္ရေအာင္။ ဥပမာဗ်ာ… item table ထဲက record အားလံုးျပမယ္ဆိုပါေတာ့။ ဒါေပမယ့္ categoryid အစား categoryname ကိုျပခ်င္တယ္ဆိုရင္၊ item table နဲ႔ category table ၂ ခုကို ခ်ိတ္ရေတာ့မွာပါ။ ဟုတ္ျပီ… ဒါဆိုရင္ table ေတြ ခ်ိတ္ဖို႔အတြက္ ဘာေတြလိုလဲ… ?

ဟုတ္ကဲ႔ table တစ္ခုနဲ႔ တစ္ခုခ်ိတ္ဆက္တာကို join လုပ္တယ္လို႔ ေခၚပါတယ္။ join နည္း ၃ မ်ိဳးရွိပါတယ္။

၁) cross join

Cross join ကေတာ့ table ၂ ခုကို comer(,) ခံျပီး join တာပါ။ ဒီလိုမ်ိဳးပါ

select item.*, category.categoryName
from item, category

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Computer
5 SONY 32” TV 3 900000 1 - Computer
6 Moon 16” Stand Fan 3 20000 3 - Computer
1 Sony Optical Mouse 1 2500 10 - Stationary
2 A4 Tech Keyboard 1 5000 1 - Stationary
3 ViewSonic 17” Flat CRT 1 15000 5 - Stationary
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Stationary
6 Moon 16” Stand Fan 3 20000 3 - Stationary
1 Sony Optical Mouse 1 2500 10 - Electronic
2 A4 Tech Keyboard 1 5000 1 - Electronic
3 ViewSonic 17” Flat CRT 1 15000 5 - Electronic
4 First Eleven Book 2 500 15 - Electronic
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic
1 Sony Optical Mouse 1 2500 10 - Clothes
2 A4 Tech Keyboard 1 5000 1 - Clothes
3 ViewSonic 17” Flat CRT 1 15000 5 - Clothes
4 First Eleven Book 2 500 15 - Clothes
5 SONY 32” TV 3 900000 1 - Clothes
6 Moon 16” Stand Fan 3 20000 3 - Clothes
1 Sony Optical Mouse 1 2500 10 - Kitchen wares
2 A4 Tech Keyboard 1 5000 1 - Kitchen wares
3 ViewSonic 17” Flat CRT 1 15000 5 - Kitchen wares
4 First Eleven Book 2 500 15 - Kitchen wares
5 SONY 32” TV 3 900000 1 - Kitchen wares
6 Moon 16” Stand Fan 3 20000 3 - Kitchen wares

cross join လို႔ ဆိုတဲ႔ အတိုင္းပါပဲ။ ဒီပံုစံက table ၂ ခုကို ေျမွာက္လို႔ရတဲ႔ အတိုင္း result ထြက္ပါတယ္။ item ထဲမွာက record ၆ ေၾကာင္း၊ category ထဲမွာက record ၅ ေၾကာင္း ရွိပါတယ္။ ဒါေၾကာင့္ အေပၚက query ကို run လိုက္ရင္ record စုစုေပါင္း အေၾကာင္း ၃၀ ထြက္ပါလိမ့္မယ္။ item table ထဲက record ၁ေၾကာင္းစီ အတြက္ category table ထဲမွာ record ရွိသေလာက္ကို ထုတ္ျပသြားတာပါ။ ကြၽန္ေတာ္က ့ အကုန္မထုတ္ပဲ item table ထဲမွာ ပါတဲ႔ categoryID နဲ႔ category table ထဲမွာပါတဲ႔ categoryID တူတဲ႔ record ေတြပဲ ထုတ္ျပခ်င္တယ္ဆိုရင္ေတာ့ ထြက္လာတဲ႔ result ကို condition ျပန္စစ္ဖို႔ လိုပါတယ္။ ဒါေၾကာင့္ ကြၽန္ေတာ္တို႔ where ကို သံုးျပီး အခုလို ေရးရပါမယ္။

select item.*, category.categoryName
from item, category where item.categoryID = category.categoryID

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic

၂) inner join
Inner join က cross join ကို where ထည့္ထားတဲ႔ ပံုစံနဲ႔ တူပါတယ္။ table ၂ ခုကို join ဖို႔အတြက္ On keyword ကိုသံုးပါတယ္။ ဒီလိုမ်ိဳးပါ..

select item.*, category.categoryName
from item inner join category
on item.categoryID = category.categoryID

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic

inner join နဲ႔ေရးထားတဲ႔အတြက္ item table ထဲက categoryID က category table ထဲမွာ ရွိမေနဘူးဆိုရင္ အဲဒီ record ကို ထုတ္ျပေပးမွာမဟုတ္ပါဘူး။ ဟုတ္ျပီ တစ္ခါတေလမွာ ကြၽန္ေတာ္က item table ထဲက အားလံုးထုတ္ျပခ်င္တယ္၊ category table ထဲမွာ ရွိရင္ category name ျပျပီး၊ မရွိရင္ေတာ့ null ပဲေပၚလာခ်င္တယ္ဆိုရင္။ တနည္းအားျဖင့္ table တခုခုကို မူတည္ျပီး ျပခ်င္တယ္ဆိုရင္ ကြၽန္ေတာ္တို႔ inner join ကိုသံုးလို႔ မရေတာ့ပါဘူး။ ဒီအခါမွာ outer join ကိုသံုးရေတာ့မွာပါ။

၃) outer join
Outer join ကို ၂ ပိုင္းထပ္ခြဲနိုင္ပါတယ္။ Left outer join နဲ႔ Right outer join ပါ။ join keyword ရဲ႕ ဘယ္ဖက္က table ကို အကုန္ျပမယ္ဆိုရင္ left join နဲ႔ ေရးရျပီး။ join keyword ရဲ႕ ညာဖက္က table ကို အကုန္ျပခ်င္ရင္ေတာ့ right join နဲ႔ ေရးပါမယ္။

select item.*, category.categoryName
from item left join category
on item.categoryID = category.categoryID

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic
select item.*, category.categoryName
from item right join category
on item.categoryID = category.categoryID

result

ItemID ItemName CategoryID Price Balance Remark CategoryName
1 Sony Optical Mouse 1 2500 10 - Computer
2 A4 Tech Keyboard 1 5000 1 - Computer
3 ViewSonic 17” Flat CRT 1 15000 5 - Computer
4 First Eleven Book 2 500 15 - Stationary
5 SONY 32” TV 3 900000 1 - Electronic
6 Moon 16” Stand Fan 3 20000 3 - Electronic
NULL NULL NULL NULL NULL NULL Clothes
NULL NULL NULL NULL NULL NULL Kitchen wares

left join လို႔ေရးေရး left outer join လို႔ပဲ ေရးေရး တူတူပါပဲ။ right join နဲ႔ right outer join လည္း တူတယ္ေနာ္။ ေနာက္ဆံုး query result မွာ null ေတြထြက္လာတာ ေတြ႔ရတယ္ေနာ္၊ ဘာလို႔လဲဆိုေတာ့ category table ကို မူတည္ျပီး ယူလိုက္ေတာ့ item table ထဲမွာ မရွိတာေတြအတြက္ null ေတြထြက္လာတာပါ။

August 3, 2009

basic of SQL Statement (part 5)

Filed under: SQL Commands — Tags: , , — sevenlamp @ 2:15 PM

အခု ကြၽန္ေတာ္တို႔ Select quary ေတြမွာ count တို႔ sum တို႔ စသည့္ျဖင့္ function ေတြထည့္သံုးၾကည္ရေအာင္။ အဲ့ဒီ function ေတြကို aggregate function ေတြလို႔ ေခၚတယ္။ ဒီလို aggregate function ေတြပါတဲ႔ query ေတြကိုလည္း aggregate query လို႔ေခၚပါတယ္။

Aggregate Functions List

AVG MIN
CHECKSUM SUM
CHECKSUM_AGG STDEV
COUNT STDEVP
COUNT_BIG VAR
GROUPING VARP
MAX

ဒီ function ေတြကို ကြၽန္ေတာ္တို႔ Select ရဲ့ ေနာက္မွာ ျဖစ္ေစ၊ Having ရဲ့ေနာက္မွာ ျဖစ္ေစ၊ သံုးနိုင္ပါတယ္။ ဥပမာ အခုကြၽန္ေတာ္က ItemTable ထဲမွာ ရွိတဲ႔ record အေရအတြက္ကို သိခ်င္တယ္ဆိုပါစို႔။ ဒါဆိုရင္ Count ဆိုတဲ႔ aggregate function ကိုသံုးျပီး ခုလိုေရးရမွာပါ။

SELECT count(*) AS ItemCount FROM item

Result:

ItemCount
6

Count ဆိုတဲ႔ function က ကြၽန္ေတာ္တို႔ Table ထဲမွာ ရွိတဲ႔ record အေရအတြက္ကိုစစ္ေပးပါတယ္။ အဲ့ဒီ function ထဲမွာ * ဒါမွမဟုတ္ ၾကိဳက္ရာ column name ထည့္ေပးရပါမယ္။ ဒီ function ေတြကိုသံုးရင္ ထြက္လာတဲ႔ result မွာ column name မရွိပါဘူး။ ဒါေၾကာင့္ ကြၽန္ေတာ္က column name ထည့္ေပးဖို႔အတြက္ AS keyword ကိုသံုးပါတယ္၊ AS ေနာက္မွာ ကြၽန္ေတာ္ေပးခ်င္တဲ႔ ItemCount ဆိုတဲ႔ နံမည္ေလးထည့္ေပးလိုက္ပါတယ္။ တျခား Function ေတြကိုလည္း စမ္းၾကည့္ရေအာင္။ ကြၽန္ေတာ္အခု စုစုေပါင္းကုန္လက္က်န္ကို သိခ်င္တယ္ဆိုရင္။

SELECT sum(balance) AS TotalBalance FROM item

Result:

TotalBalance
35

အခုကြၽန္ေတာ္တို႔ aggregate function ေတြကိုသံုးတဲ႔အခါမွာ table ထဲက record အားလံုးအတြက္ result တစ္ခုထဲထြက္တာကို ေတြ႔ရလိမ့္မယ္။ ဟုတ္ျပီ ဒါဆိုရင္ အခုကြၽန္ေတာ္က Category တစ္ခုစီအတြက္ ကုန္လက္က်န္ကို ၾကည့္ခ်င္တယ္ဆိုရင္ေရာ။ ဒီလိုလုပ္ဖို႔ဆိုရင္ေတာ့ ကြၽန္ေတာ္တို႔ Group By keyword နဲ႔ တြဲသံုးေပးဖို႔လိုလာပါျပီ။

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid

Result:

categoryID TotalBalance
1 16
2 15
3 4

*** မွတ္ထားစရာ အခ်က္တစ္ခ်က္ရွိေသးတယ္ဗ်။ select ထဲမွာ aggregate function ေတြ ပါျပီဆိုရင္၊ Group By လုပ္ထားတဲ႔ column ေတြကလြဲျပီး တျခား column ေတြကို ထည့္ေရးခြင့္ မရွိပါဘူး***
အခု Having keyword အေၾကာင္းဆက္ေျပာရေအာင္။ ကြၽန္ေတာ္တို႔ေရးတဲ႔ query မွာ condition စစ္စရာေတြ ရွိရင္ where ကိုသံုးရတယ္ဆိုတာ သိျပီးၾကျပီေနာ္။ ဟုတ္ျပီ အဲ့ဒီ where keyword က database ထဲမွာ ရွိတဲ႔ column name ေတြကိုပဲ စစ္လို႔ရပါတယ္။ တခါတေလကြၽန္ေတာ္တို႔က aggregate function ကထြက္လာတဲ႔ result ေတြကို စစ္ဖို႔လိုလာျပီဆိုရင္ေတာ့ Having ကိုသံုးရေတာ့မွာပါပဲ။ ဥပမာ ကုန္လက္က်န္ စုစုေပါင္း ၁၀ ခုထက္နည္းနဲ႔ category ကိုၾကည့္ခ်င္တယ္ဆိုရင္။

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid
HAVING sum(balance) < 10

Result:

categoryID TotalBalance
3 4

Having ကို where လိုမ်ိဳးလဲ column ေတြစစ္ဖို႔အတြက္ သံုးလို႔လည္း ရပါတယ္။ category id 1 ရဲ႕ totalbalance ကိုျပဖို႔အတြက္

SELECT categoryid,sum(balance) AS TotalBalance
FROM item
GROUP BY categoryid
HAVING categoryid = 1

Result:

categoryID TotalBalance
1 16

July 31, 2009

basic of SQL Statement (part 4)

Filed under: SQL Commands — Tags: , , , — sevenlamp @ 2:21 PM

ဒီေန႔ Select Statement အေၾကာင္းပဲ ဆက္ရေအာင္ဗ်ာ။ WHERE အေၾကာင္းပဲ ဆက္ေျပာပါ့မယ္။ အခုကြၽန္ေတာ္က ကုန္လက္က်န္ ၁၀ ခုထက္နည္းျပီး ေစ်းႏႈန္းကလည္း ၁၀၀၀၀ အထက္ရွိတဲ႔ item ေတြရဲ့ list ကိုၾကည့္ခ်င္တယ္။ ဒါဆိုရင္ ဒီလိုေရးရမွာေပါ့…

SELECT * FROM item
WHERE balance < 10 AND price > 10000

Result :

ItemID ItemName CategoryID Price Balance Description
3 ViewSonic 17” Flat CRT 11 15000 5 -
5 SONY 32” TV 3 900000 1 -
6 Moon 16” Stand Fan 3 20000 3 -

ဒီ query ေလးမွာ ကြၽန္ေတာ္စစ္ခ်င္တဲ႔ condition က ၂ ခုရွိေနတယ္။ balance ၁၀ ခုထက္နည္းရမွာရယ္၊ price ၁၀၀၀၀ ထက္မ်ားရမွာရယ္။ အဲ့ဒီ condition ၂ ခုလံုးမွန္တာကိုပဲ လိုခ်င္တာ ျဖစ္ေတာ့ Where ေနာက္မွာ condition ၂ ခုကို AND နဲ႔ဆက္ျပီးေရးရပါတယ္။ ကြၽန္ေတာ္တို႔ စစ္ခ်င္တဲ႔ condition ေတြ ၁ခုထက္ပိုမ်ားလာျပီဆိုရင္ အဲ့ဒီ condition ေတြၾကားမွာ AND ဒါမွမဟုတ္ OR ဆိုတဲ႔ keyword ေလးေတြထည့္ေရးေပးရမယ္။ အေပၚက query လိုမ်ိဳး condition ၂ ခုလံုးမွန္မွ result ထြက္ခ်င္တယ္ဆိုရင္ AND ကိုသံုးရျပီး။ condition တစ္ခုမဟုတ္ တစ္ခုမွန္ရံုနဲ႔ result ထြက္ခ်င္တယ္ဆိုရင္ေတာ့ OR ကိုသံုးရမွာပါ။ ဥပမာ အေနနဲ႔ ကြၽန္ေတာ္က electronic ပစၥည္းေတြနဲ႔ computer ပစၥည္းေတြကိုပဲ ၾကည့္ခ်င္တယ္ ဆိုပါစို။ ဒါဆို ဒီလိုေရးရမွာပါ…

SELECT * FROM item
WHERE CategoryID = 1 OR CategoryID = 3

Result :

ItemID ItemName CategoryID Price Balance Description
1 Sony Optical Mouse 1 2500 10 -
2 A4 Tech Keyboard 1 5000 1 -
5 SONY 32” TV 3 900000 1 -
6 Moon 16” Stand Fan 3 20000 3 -

တခါတေလမွာ ကြၽန္ေတာ္တို႔စစ္ခ်င္တဲ႔ condition က value ၂ ခုၾကားျဖစ္ခဲ႔ရင္ BETWEEN ကိုလည္း သံုးလို႔ရပါတယ္။ ဥပမာ balance ၂ ခုကေန ၁၀ ခု ၾကားထဲက record ေတြ လိုခ်င္တယ္ဆိုရင္…

SELECT * FROM item
WHERE balance BETWEEN 2 AND 10

Result :

ItemID ItemName CategoryID Price Balance Description
1 Sony Optical Mouse 1 2500 10 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
6 Moon 16” Stand Fan 3 20000 3 -

ထြက္လာတဲ႔ result ရဲ့ ဆန္က်င္ဘက္ ေျပာင္းျပန္ကို လိုခ်င္တယ္ ဆိုရင္ေတာ့ NOT ကို တြဲသံုးလို႔ ရပါတယ္။

SELECT * FROM item
WHERE balance NOT BETWEEN 2 AND 10

Result:

ItemID ItemName CategoryID Price Balance Description
2 A4 Tech Keyboard 1 5000 1 -
4 First Eleven Book 2 500 15 -
5 SONY 32” TV 3 900000 1 -

တစ္ခါတစ္ေလ ကြၽန္ေတာ္တို႔ လိုခ်င္တဲ႔ record ေတြက အစီအစဥ္လိုက္မဟုတ္ဘူး ဆိုရင္ေတာ့ condition စစ္တဲ႔အခါ OR ေတြအမ်ားၾကီးသံုးရမယ့္ အေနအထားေတြ ရွိလာနိုင္ပါတယ္။ ဥပမာ ကြၽန္ေတာ္က ItemID 1,3,5 ကိုထုတ္ၾကည့္မယ္ဆိုရင္ OR သံုးျပီးစစ္ရင္ ဒီလိုမ်ိဳးျဖစ္ပါမယ္။

SELECT * FROM item
WHERE ItemID = 1 OR ItemID = 3 OR ItemID = 5

ဒီလို အေျခအေနမ်ိဳးမွာ ကြၽန္ေတာ္တို႔ IN ကိုသံုးျပီးေရးရင္ ပိုလြယ္မွာပါ။

SELECT * FROM item
WHERE ItemID IN ( 1,3,5 )

Result:

ItemID ItemName CategoryID Price Balance Description
1 Sony Optical Mouse 1 2500 10 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
5 SONY 32” TV 3 900000 1 -

ItemID 1,3,5 ကလြဲျပီး က်န္တာေတြ ျပမယ္ဆိုရင္ေတာ့

SELECT * FROM item
WHERE ItemID NOT IN ( 1,3,5 )

Result:

ItemID ItemName CategoryID Price Balance Description
2 A4 Tech Keyboard 1 5000 1 -
4 First Eleven Book 2 500 15 -
6 Moon 16” Stand Fan 3 20000 3 -

ဒီေလာက္ဆိုရင္ေတာ့ ကြၽန္ေတာ္တို႔ WHERE Condition ေတာ္ေတာ္ေလးစစ္တက္ျပီလို႔ ေျပာလို႔ရပါျပီ။ အခုေနာက္ထပ္ ORDER BY အေၾကာင္း ဆက္ေျပာပါ့မယ္။ Select Query ကေနထြက္လာတဲ႔ result record ေတြကို sorting လုပ္ခ်င္ရင္ Order By keyword ကိုသံုးရမွာပါ။ အိုေက အခုကြၽန္ေတာ္ Item table ထဲက record ေတြအားလံုးၾကည့္ခ်င္တယ္။ ကုန္လက္က်န္အနည္းဆံုးက အေပၚဆံုးမွာျပခ်င္တယ္ဆိုရင္….

SELECT * FROM item
ORDER BY balance

Result:

ItemID ItemName CategoryID Price Balance Description
2 A4 Tech Keyboard 1 5000> 1 -
5 SONY 32” TV 3 900000 1 -
6 Moon 16” Stand Fan 3 20000 3 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
1 Sony Optical Mouse 1 2500 10 -
4 First Eleven Book 2 500 15 -

Order by keyword က default အားျဖင့္ ငယ္စဥ္ၾကီးလိုက္(Ascending) စီေပးပါတယ္။ ၾကီးစဥ္ငယ္လိုက္(Descending) စီခ်င္တယ္ဆိုရင္ေတာ့ ကိုယ္စီခ်င္တဲ႔ column name ေနာက္မွာ DESC ဆိုျပီးထည့္ေပးရပါမယ္။ ဥပမာ ကုန္လက္က်န္အမ်ားဆံုးကို အေပၚဆံုးကေန ျပခ်င္ရင္…

SELECT * FROM item
ORDER BY balance DESC

Result:

ItemID ItemName CategoryID Price Balance Description
4 First Eleven Book 2 500 15 -
1 Sony Optical Mouse 1 2500 10 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
6 Moon 16” Stand Fan 3 20000 3 -
2 A4 Tech Keyboard 1 5000 1 -
5 SONY 32” TV 3 900000 1 -

အခုလို sorting စီတဲ႔ေနရာမွာ Column တစ္ခုထက္ပိုျပီး စီခ်င္ရင္ comer(,) ေလးေတြ ျခားသြားရံုပါပဲ။ ကုန္လက္က်န္ အနည္းဆံုးကို အရင္စီမယ္၊ ကုန္လက္က်န္ အေရအတြက္ခ်င္း တူရင္ ေစ်းႏႈန္းအမ်ားဆံုးကို အေပၚကထားခ်င္တယ္ဆိုရင္….

SELECT * FROM item
ORDER BY Balance ASC, Price DESC

(*** ASC ကို ထည့္ေရးေရး၊ မေရးေရး ရပါ၏ ***)

Result:

ItemID ItemName CategoryID Price Balance Description
5 SONY 32” TV 3 900000 1 -
2 A4 Tech Keyboard 1 5000 1 -
6 Moon 16” Stand Fan 3 20000 3 -
3 ViewSonic 17” Flat CRT 11 15000 5 -
1 Sony Optical Mouse 1 2500 10 -
4 First Eleven Book 2 500 15 -

ဒီေလာက္ဆိုရင္ေတာ့ ကြၽန္ေတာ္တို႔ Simple Select Query ေတြ ေရးတက္ပါျပီ။

July 30, 2009

basic of SQL Statement (part 3)

Filed under: SQL Commands — Tags: , — sevenlamp @ 6:16 PM

Select Statement

SELECT [ALL | DISTINCT] columnname1 [,columnname2]
FROM tablename1 [,tablename2]
[WHERE condition] [ and|or condition...]
[GROUP BY column-list]
[HAVING "conditions]
[ORDER BY "column-list" [ASC | DESC] ]

SQL Statement ၄ မ်ိဳးမွာျဖင့္ ဒီ select က အရွဳပ္ဆံုးပါပဲဗ်ာ။ user ၾကည့္ခ်င္တဲ႔ result ရဖို႔အတြက္ ကြၽန္ေတာ္တို႔ select command ကို နည္းလမ္းမ်ိဳးစံုနဲ႔ သံုးရမွာပါ။ အရႈပ္ဆံုးဆိုလို႔လည္း လန္႔မသြားနဲ႔ဦးေနာ္၊ ဘယ္အရာမဆို မသိရင္ခက္ျပီး သိသြားရင္ေတာ့ လြယ္ပါတယ္။ ကဲ.. အလြယ္ဆံုးကေန စလိုက္ရေအာင္။ ပထမဆံုး ကြၽန္ေတာ္က Category Table ထဲက record ေတြအားလံုးကို user ကို ထုတ္ျပခ်င္တယ္ ဆိုပါေတာ့။ ဒါဆိုရင္ ဒီလိုေရးရမွာပါ။

SELECT * FROM Category

Result :

CategoryID CategoryName Status
1 Computer Active
2 Stationary Active
3 Electronic Active
4 Clothes Inactive
5 Kitchen wares Inactive

Select command ရဲ႕ ေနာက္မွာ ကိုယ္ထည့္ၾကည့္ခ်င္တဲ႔ column name ေတြေပးရမွာပါ။ ရွိသမွ် ေကာ္လံအားလံုးကို ထုတ္ျပမယ္ဆိုရင္ ကြၽန္ေတာ္အခုေရးလိုက္သလို (*) ကို သံုးလို႔လည္း ရပါတယ္။ ဒါဆို အခုကြၽန္ေတာ္က CategoryID နဲ႔ CategoryName ၂ ခုပဲ ၾကည့္ခ်င္တယ္ဆိုရင္ ဒီလိုျဖစ္သြားမယ္။

SELECT CategoryID,CategoryName FROM Category

Result :

CategoryID CategoryName
1 Computer
2 Stationary
3 Electronic
4 Clothes
5 Kitchen wares

ဘယ္လိုလဲ လြယ္ပါတယ္ေနာ္။ လြယ္မွာေပါ့ ဒါက အရိုးရွင္းဆံုး select ပံုစံကို။ ကဲ အခုကြၽန္ေတာ္က category table ထဲက record အားလံုးမၾကည့္ခ်င္ဘူး Status က Active ျဖစ္တဲ႔ Category ေတြပဲ ၾကည့္ခ်င္တယ္ ဆိုရင္ Condition ျပန္စစ္ဖို႔လိုလာပါျပီ။ select command မွာ condition စစ္ဖို႔အတြက္ where ရယ္၊ having ရယ္ ၂ ခု ၾကိဳက္တာသံုးခြင့္ရွိပါတယ္။ ဒီ ၂ ခုဘာကြာလဲဆိုတာ ေနာက္မွေျပာျပပါမယ္။ ေလာေလာဆယ္ေတာ့ ကြၽန္ေတာ္က where ကိုပဲ သံုးျပီး စစ္လုိက္ပါမယ္။

SELECT * FROM Category WHERE Status = ‘Active’

Result :

CategoryID CategoryName Status
1 Computer Active
2 Stationary Active
3 Electronic Active

ဒါဆိုရင္ေတာ့ record ၃ ေၾကာင္းပဲ ျပပါေတာ့မယ္။

နက္ဖန္မွဆက္ေရးေတာ့မယ္ဗ်ာ။ ဒီေန႔ ျပန္ရေတာ့မွာမို႔လို႔။

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.