SQL, MySQL


selet table.field

from table

where เงื่อนไข

group by field

having เงื่อนไข

order by field

limit index start,จำนวน record

------------------------------------------------------------------

select p.products_id,

p.products_price,

p.products_price*35

from products as p

-------------------------------------------------------------------

select count(*) as total,

min(p.products_price) as min,

max(p.products_price) as max

from products as p

-------------------------------------------------------------------

select p.products_id,

p.products_model,

p.products_price

from products as p

where p.products_price>=50

-------------------------------------------------------------------

select p.products_id,

p.products_model,

p.products_price

from products as p

where p.products_price between 50 and 100

-------------------------------------------------------------------

select p.products_id,

p.products_model,

p.products_price

from products as p

where p.products_id not in(1,3,5)

-------------------------------------------------------------------

select p.products_id,

p.products_model,

p.products_price

from products as p

where p.products_model like '%speed%'

-------------------------------------------------------------------

select pi.*

from products_images as pi

where pi.htmlcontent is not null

-------------------------------------------------------------------

select p.manufacturers_id, count(*) as total,

min(p.products_price) as min,

max(p.products_price) as max,

avg(p.products_price) as avg

from products as p

group by p.manufacturers_id

having count(*)>=3

order by desc

-------------------------------------------------------------------

select p.manufacturers_id, count(*) as total,

min(p.products_price) as min,

max(p.products_price) as max,

avg(p.products_price) as avg

from products as p

group by p.products_price desc

-------------------------------------------------------------------

select *

from products

limit 0,10

-------------------------------------------------------------------

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

select p.products_id,

p.products_model,

p.products_price,

pd.products_name

from products as p,

products_description as pd

where p.products_id=pd.products_id

and pd.language_id=1

order by p.products_id

---------------------------------------------------------

select p.products_id,

p.products_model,

p.products_price,

pd.products_name,

m.manufacturers_name

from products as p,

products_description as pd,

manufacturers as m

where p.products_id=pd.products_id

and p.manufacturers_id=m.manufacturers_id

and pd.language_id=1

order by p.products_id

---------------------------------------------------------

select p.products_id,

p.products_model,

p.products_price,

pd.products_name,

m.manufacturers_name

from products as p

join products_description as pd

on p.products_id=pd.products_id

left join manufacturers as m

on p.manufacturers_id=m.manufacturers_id

where p.products_id=pd.products_id

and pd.language_id=1

order by p.products_id

---------------------------------------------------------

select m.manufacturers_id,

m.manufacturers_name,

(select count(*) from products

where manufacturers_id=m.manufacturers_id)

as total

from manufacturers as m

order by m.manufacturers_id

----------------------------------------------------------

select p.products_id,

p.products_model,

p.products_price,

pd.products_name,

m.manufacturers_name

from products as p

join products_description as pd

on p.products_id=pd.products_id

left join manufacturers as m

on p.manufacturers_id=m.manufacturers_id

where p.products_id=pd.products_id

and pd.language_id=1

and p.products_id in (select products_id

from products_to_categories

where categories_id=12)

order by p.products_id

----------------------------------------------------------

คำสำคัญ (Tags): #sql
หมายเลขบันทึก: 600060เขียนเมื่อ 27 มกราคม 2016 15:44 น. ()แก้ไขเมื่อ 27 มกราคม 2016 17:04 น. ()สัญญาอนุญาต: ครีเอทีฟคอมมอนส์แบบ แสดงที่มา-ไม่ใช้เพื่อการค้า-ไม่ดัดแปลงจำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท