SELECT column 1 column 2 FROM tablename SELECT
คำสง เปนคำสงเลอกขอมลจาก ������ SELECT column 1, column 2, . . . FROM table_name; SELECT * FROM table_name Select Table ����������� Field name ������ Field ������� เชน SELECT Product_id, Product_name. Eng from T_Product เชน SELECT * FROM T_Product
ลำดบการเขยน Select >> From >> Where >> Order by Select >> From >> Where >> group by
SELECT DISTINCT select Receipt_no from T_Sales_Detail Receipt_No F 16012019 G 300001 F 16012019 G 300001 F 16012019 G 300002 F 17012019 G 300001 F 17012019 G 300002 SELECT DISTINCT(Receipt_no) from T_Sales_Detail Receipt_No F 16012019 G 300001 F 16012019 G 300002 F 17012019 G 300001 F 17012019 G 300002
การใสเงอนไข Where SELECT column 1, column 2, . . . FROM table_name WHERE condition 1 and condition 2 and condiion 3 SELECT column 1, column 2, . . . FROM table_name WHERE condition 1 or condition 2 or condition 3…
การใสเงอนไข Where select Product_Id, Product_Name. Eng, Unit_Price, Product_Type_Id from T_Product where Unit_Price >=3000 select Product_Id, Product_Name. Eng, Unit_Price, Product_Type_Id from T_Product where Unit_Price =3000
สรางเงอนไข And, Or, Not SELECT column 1, column 2, . . . FROM table_name WHERE condition 1 and condition 2 and condiion 3 SELECT column 1, column 2, . . . FROM table_name WHERE condition 1 or condition 2 or condition 3… SELECT column 1, column 2, . . . FROM table_name WHERE NOT condition; Or = ขอมลทตรงเงอนไขอยางใดอยางหนงกใหแสดงข And = อมล ขอมลทตรงเงอนไขทงสองอยางจงใหแสดงขอม Not = คอขอมลทไมอยในเงอนไข ล
สรางเงอนไข select Product_Id, Product_Name. Thai, Unit_Price, Product_Type_Id from T_Product where Unit_Price >50 and Product_Type_Id='PT 006' Product_Id Product_Name. Thai G 012 เนอ G 015 นม Unit_Price 190 60 Product_Type_Id PT 006 And, Or, Not select Product_Id, Product_Name. Thai, Unit_Price, Product_Type_Id from T_Product where Unit_Price >50 or Product_Type_Id='PT 006' Product_Id G 001 G 002 G 003 G 004 G 006 G 007 G 008 G 009 G 011 G 012 G 013 G 014 G 015 G 016 G 017 G 018 G 019 Product_Name. Thai เบยร ผาหม ขนมปง ลกอม คอมพวเตอร คกก ผาออม ไข เครองสำอาง เนอ ยาลดไข ไมโครเวฟ นม เครองพมพ ยาสระผม โทรศพท โทรทศน Unit_Price 60 20 10 20000 40 150 20 200 190 290 3000 60 2000 60 30000 20000 Product_Type_Id PT 001 PT 009 PT 006 PT 008 PT 006 PT 009 PT 006 PT 002 PT 006 PT 004 PT 008 PT 006 PT 008 PT 009 PT 008
สรางเงอนไข And, Or, Not select Product_Id, Product_name. Eng from T_Product where not Product_name. Eng ='Beer' select Product_Id, Product_name. Eng , Unit_Price from T_Product where Unit_Price >10 and (Product_name. Eng ='Beer' or Product_name. Eng='Coke')
Order by …Desc|Asc select Product_Id, Product_name. Eng, Unit_price from T_Product order by unit_price desc select Product_Id, Product_name. Eng, Unit_price from T_Product order by unit_price asc
คำสง INSERT SQL Command Description INSERT INTO table_name (column 1, column 2, column 3, . . . ) VALUES (value 1, value 2, value 3, . . . ) ระบ Field ขอมลทใสใน ฐานขอมล กรณนไมตอง ใสครบทก Field ใน Table ไมระบ Field nameแตกรณนต องใสขอมลท ก Field ใหครบทก INSERT INTO table_name VALUES (value 1, value 2, value 3, . . . )
คำสง INSERT SQL Command insert into Color(Color_Id, Color_Desc) values('CL 006', 'Black‘) Description สำหรบตองการระบขอมล ทใสในฐานขอมล (ครบทก Field ใน Table) insert into Color(Color_Id) values('CL 010') ใสขอมล 1 Field ตามตองการ และระบ Field name (ไมครบทก Field ใน Table) insert into Color values('CL 007', 'Yellow‘) กรณไมระบ Filed name กรณนตองใสขอมล ให ครบทก Field insert into Color values('CL 009‘) � ERROR
Null, Not Null SQL Command SELECT column_names FROM table_name WHERE column_name IS NULL SELECT column_names FROM table_name WHERE column_name IS NOT NULL; Description เลอกขอมลทค า เปน Null เลอกขอมลทค า ไมเปน Null
Null, Not Null SQL Command select * from Brand where brand_name is null select * from Brand where brand_name is not null Description เลอกขอมลทค า เปน Null เลอกขอมลทค า ไมเปน Null
คำสง Update UPDATE table_name SET column 1 = value 1, column 2 = value 2, . . . WHERE condition; Update Table. Name Set Field. Name 1=Value 1, Field. Name 2=value 2, … [Where condition]
คำสง Update T_Product Set Product_Name. Eng='Water' (!!! คำสงนจะแกไขทง Table) พงระวงเสมอ Update T_Product Set Product_Name. Eng='Water', Product_Name. Thai='นำเปลา ', Unit_Price=70 Where Product_id='G 001' Data Type Varchar, char ������ ‘’
คำสง Delete DELETE FROM table_name WHERE [condition] SQL Command Delete From T_Product Where Product_id='G 001' Description (!!! ������������ Table) ������ ลบขอมลตามเงอนไข
คำสง SQL Command Delete From TPS_Product Where Product_id='G 001' Delete Description (!!! ������������ Table) ������ ลบขอมลตามเงอนไข
Select Top SELECT TOP number|percent column_name(s) FROM table_name WHERE condition; SELECT column_name(s) FROM table_name WHERE condition LIMIT number; **** ไปลองเองเพราะ Run ไมสำเรจ
Select Top SELECT TOP 3 * FROM T_Product SELECT TOP 50 PERCENT * FROM T_Product
Select …Function Select Min (นอยสด ), Select Max (มากสด ), Select AVG (คาเฉลย ), Select Count (จำนวนนบ ), Select Sum (จำนวนรวม )
Select MIN, MAX SELECT MIN(column_name) FROM table_name WHERE condition; SELECT MAX(column_name) FROM table_name WHERE condition;
Select MIN, MAX select MIN(Unit_price) from T_Product select MAX(Unit_price) from T_Product where product_type_id='PT 001'
Select Count, AVG, SUM SELECT COUNT(column_name) FROM table_name WHERE condition SELECT AVG(column_name) FROM table_name WHERE condition SELECT SUM(column_name) FROM table_name WHERE condition
Select Count, AVG, SUM select COUNT(Product_Id) from T_Product where product_type_id='PT 001' select AVG(unit_price) from T_Product where product_type_id='PT 001' select sum(unit_price) from T_Product where product_type_id='PT 001‘ Product_Id G 001 G 005 G 010 G 020 G 011 G 013 G 012 G 015 G 007 G 003 G 004 G 009 G 006 G 018 G 019 G 014 G 017 G 008 G 002 Product_Name. Eng Unit_Price Product_Type_Id Beer 60 PT 001 Coke 12 PT 001 Green Tea 20 PT 001 Water 12 PT 001 Make up 200 PT 002 Medicine 290 PT 004 Meat 190 PT 006 milk 60 PT 006 Cookie 40 PT 006 Bread 20 PT 006 Candy 10 PT 006 Egg 20 PT 006 Computer 20000 PT 008 Printer 2000 PT 008 Telephone 30000 PT 008 Television 20000 PT 008 Microwave 3000 PT 008 Shampoo 60 PT 009 Diaper 150 PT 009 Blanket 200 PT 009
LIKE SELECT column 1, column 2, . . . FROM table_name WHERE column. N LIKE pattern;
LIKE Operator Description WHERE Customer. Name LIKE 'a%' Finds any values that start with "a" WHERE Customer. Name LIKE '%a' Finds any values that end with "a" WHERE Customer. Name LIKE '%or%' Finds any values that have "or" in any position WHERE Customer. Name LIKE '_r%' Finds any values that have "r" in the second position WHERE Customer. Name LIKE 'a__%' Finds any values that start with "a" and are at least 3 characters in length WHERE Contact. Name LIKE 'a%o' Finds any values that start with "a" and ends with "o"
LIKE select Product_Id, Product_name. Eng, Unit_price, Product_Type_Id from T_Product where Product_Name. Eng like'B%’ >>>> (หมายถง ชอทขนตนดวยอกษร B) select Product_Id, Product_name. Eng, Unit_price, Product_Type_Id from T_Product where Product_Name. Eng like'%B%' >>>> (หมายถง ชอทมอกษร อยตำแหนงกลางระหวาง อกษร ) select Product_Id, Product_name. Eng, Unit_price, Product_Type_Id from T_Product where Product_Name. Eng like'%B' >>>> (หมายถง ชอทลงทายดวยอกษร B B)
Select … Where value in … SELECT column_name(s) FROM table_name WHERE column_name IN (value 1, value 2, . . . ); SELECT column-name FROM table-name 1 WHERE column-name IN (SELECT STATEMENT) https: //www. dofactory. com/s ql/subquery
Select … Where value in … select Product_Id, Product_name. Eng, Unit_price, Product_Type_Id from T_Product where Product_Name. Eng in ('Beer', 'Blanket', 'Bread') select Product_Id, Product_name. Eng, Unit_price, Product_Type_Id from T_Product where Product_Name. Eng not in ('Beer', 'Blanket', 'Bread')
Select … Where value in … SELECT column-names หรอ SELECT * FROM table-name 1 WHERE value IN (SELECT column-name FROM table-name 2 WHERE condition) select * from T_Sales_detail where Product_Id in (Select Product_Id from T_Product where Product_Type_Id ='PT 001') order by Receipt_No asc
Select … Where value in … select * from T_Sales_detail where Product_Id in (Select Product_Id from T_Product where Product_Type_Id ='PT 001') order by Receipt_No asc select A. Receipt_no, A. Product_Id, A. Product_name. Eng, A. Qty, A. Unit_Price, A. Total_amt, B. Product_Type_Id from T_Sales_detail A, T_Product B where B. Product_Type_Id ='PT 001' and A. Product_Id=B. Product_Id order by Receipt_No asc
Between SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value 1 AND value 2
Between select Product_Id, Product_name. Eng, Unit_price, Product_Type_Id from T_Product where Unit_price between 20 and 50 เราสามารถเขยนในรปแบบอนไดซงทำใหผลลพธเหม อนกน select Product_Id, Product_name. Eng, Unit_price, Product_Type_Id from T_Product where Unit_price >= 20 and Unit_price<=50 Between And A, B
Not Between select Product_Id, Product_name. Eng, Unit_price, Product_Type_Id from T_Product where Unit_price not between 20 and 50
Alias (นามแฝง): Alias Column Syntax SELECT column_name AS alias_name FROM table_name; select Product_Id as Id, Product_name. Eng as Name from T_Product
Alias (นามแฝง): Alias Table Syntax SELECT column_name(s) FROM table_name AS alias_name; select A. Product_Id , A. Product_name. Eng from T_Product as A
- Slides: 42