SQLのメモ




SELECT

SELECT staff_id, staff_name
FROM staff
WHERE team = 1 AND rank > 3


UPDATE

UPDATE staff
SET rank = 3
WHERE staff_id = 1

DELETE

DELETE
FROM staff
WHERE staff_id = 1

INSERT

INSERT INTO staff
(staff_id, staff_name, team, rank)
VALUES (1, 'tarou', 2, 3)

INNER JOIN

SELECT *
FROM staff
INNER JOIN department
ON department.department_id = staff.department_id
AND department.department_id = 1
WHERE staff.rank > 3

サブクエリ

SELECT shop.shopname
FROM shop
WHERE shop.ownerid =
(SELECT staff.staffid
FROM staff
WHERE staff.rank > 3)

FROMに複数テーブル

SELECT staff.*, department.*
FROM staff, department
WHERE department.department_id = staff.department_id
AND department.department_id = 1
AND staff.rank > 3

LIMIT

SELECT *
FROM staff
LIMIT 10

TOP

SELECT TOP 10 *
FROM staff

ROWNUM

SELECT TOP 10 *
FROM staff
WHERE ROWNUM <= 10

GROUP BY

SELECT COUNT(staff.staffid)
FROM staff
WHERE staff.rank > 3
GROUP BY staff.rank

SELECT COUNT(staff.staffid)
FROM staff
GROUP BY staff.rank
HAVING staff.rank > 3

UNION

SELECT id, name
FROM fruits
WHERE price > 300
UNION ALL
SELECT id, name
FROM vegetables
WHERE price > 100






桜の扉