https://www.tutorialrepublic.com/codelab.php?topic=sql&file=select-all
依照各部門薪水總和最高到薪水總和最低,依序列出部門員工薪資總和大於10000的部門ID、部門薪資總和。沒有部門編號的員工(dept_id <> 'null') 不包含在資料的統計結果內
SELECT dept_id,
SUM(salary) AS "薪資總和"
FROM employees
WHERE dept_id IS NOT NULL
GROUP BY dept_id
HAVING SUM(salary) > 10000
ORDER BY "薪資總和"
統計前的分類→統計前後的過濾條件→挑選被統計與沒有被統計的欄位
FROM employees
GROUP BY dept_id
WHERE dept_id IS NOT NULL
GROUP BY dept_id
HAVING SUM(salary) > 10000
ORDER BY "薪資總和"
SELECT dept_id,
SUM(salary) AS "薪資總和"
WHERE dept_id IS NOT NULL
GROUP BY dept_id
HAVING SUM(salary) > 10000
ORDER BY "薪資總和"
在sql的數字比較跟大多數的C語言程式語言相同,不過有兩點需要注意:
(一) 相等 與 不相等
1.相等:= (一個等號)
例如,A欄位是否等於B欄位: A = B
2.不相等:<> (大於小於)
例如,A欄位是否不等於B欄位:A <> B
(二) IS NULL 和 IS NOT NULL
在資料庫系統當中,不是每個欄位都會有值
如果沒有值,在資料庫以「NULL」表示「空值」
1.判斷是空值:IS NULL
例如,A欄位 是否為 空值:A IS NULL
2.判斷是否有值: IS NOT NULL
例如,A 欄位 是否有值:A IS NOT NULL
建立表格與新增資料的DDL SQL
CREATE TABLE Family (
`FamilyId` VARCHAR(100) PRIMARY KEY,
`FamilyName` VARCHAR(5),
`FamilySex` VARCHAR(2),
`BirthDate` DATETIME,
`PhoneNumber` VARCHAR(10)
) CHARACTER SET=utf8;
INSERT INTO Family
(`FamilyId`, `FamilyName`, `FamilySex`, `BirthDate`, `PhoneNumber`)
VALUES
('34bf1b6f-191d-40e9-9a8c-3c282e6a700d', '泰肝', '女生', '2007-10-11', 'NULL'),
('3ad93ba4-c799-4a32-ac2e-8abc74dd6375', '泰熱', '男生', '2001-12-10', '0934567890'),
('91b18f1f-4ef8-4066-97c4-28daea585db5', '泰胖', '女生', '2003-05-13', 'NULL'),
('91dcde4b-10b3-421e-ab8e-bb6bc23b4350', '泰瘦', '女生', '1985-07-10', '0944623456'),
('bce0e4ae-ac70-4131-aa6f-d1e25b87fad9', '泰冷', '男生', '1995-01-23', '0977654258'),
('de8cb5db-2061-4d35-a662-ba5f528fadba', '泰賢', '男生', '1977-02-03', '0944589456');
CREATE TABLE [dbo].[Family](
[FamilyId] [nvarchar](100) NOT NULL,
[FamilyName] [nvarchar](10) NULL,
[FamilySex] [nvarchar](10) NULL,
[BirthDate] [datetime] NULL,
[PhoneNumber] [nvarchar](20) NULL,
CONSTRAINT [PK_Family] PRIMARY KEY CLUSTERED
(
[FamilyId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
INSERT [dbo].[Family] ([FamilyId], [FamilyName], [FamilySex], [BirthDate], [PhoneNumber]) VALUES (N'34bf1b6f-191d-40e9-9a8c-3c282e6a700d', N'泰肝', N'女生', CAST(0x000099C400000000 AS DateTime), NULL)
INSERT [dbo].[Family] ([FamilyId], [FamilyName], [FamilySex], [BirthDate], [PhoneNumber]) VALUES (N'3ad93ba4-c799-4a32-ac2e-8abc74dd6375', N'泰熱', N'男生', CAST(0x0000917100000000 AS DateTime), N'0934567890')
INSERT [dbo].[Family] ([FamilyId], [FamilyName], [FamilySex], [BirthDate], [PhoneNumber]) VALUES (N'91b18f1f-4ef8-4066-97c4-28daea585db5', N'泰胖', N'女生', CAST(0x0000937800000000 AS DateTime), NULL)
INSERT [dbo].[Family] ([FamilyId], [FamilyName], [FamilySex], [BirthDate], [PhoneNumber]) VALUES (N'91dcde4b-10b3-421e-ab8e-bb6bc23b4350', N'泰瘦', N'女生', CAST(0x00007A0400000000 AS DateTime), N'0944623456')
INSERT [dbo].[Family] ([FamilyId], [FamilyName], [FamilySex], [BirthDate], [PhoneNumber]) VALUES (N'bce0e4ae-ac70-4131-aa6f-d1e25b87fad9', N'泰冷', N'男生', CAST(0x000087A000000000 AS DateTime), N'0977654258')
INSERT [dbo].[Family] ([FamilyId], [FamilyName], [FamilySex], [BirthDate], [PhoneNumber]) VALUES (N'de8cb5db-2061-4d35-a662-ba5f528fadba', N'泰賢', N'男生', CAST(0x00006DFD00000000 AS DateTime), N'0944589456')
列出每位家庭成員的「姓名」、「生日」,並根據成員的出生年月日計算並顯示各自的「年齡」。
SELECT Family.FamilyName AS "姓名",
CAST(Family.BirthDate AS DATE) AS "生日 (DATE 預設格式)",
CONCAT(YEAR(Family.BirthDate) , '年' , MONTH(Family.BirthDate) , '月' , DAY(Family.BirthDate), '日') AS "生日(中文)",
FLOOR(DATEDIFF(now(), Family.BirthDate) / 365.25) AS "年齡"
FROM Family;
姓名 | 生日 (DATE 預設格式) | 生日(中文) | 年齡 |
---|---|---|---|
泰肝 | 2007-10-11 | 2007年10月11日 | 11 |
泰熱 | 2001-12-10 | 2001年12月10日 | 17 |
泰胖 | 2003-05-13 | 2003年5月13日 | 16 |
泰瘦 | 1985-07-10 | 1985年7月10日 | 34 |
泰冷 | 1995-01-23 | 1995年1月23日 | 24 |
泰賢 | 1977-02-03 | 1977年2月3日 | 42 |
SELECT Family.FamilyName AS "姓名",
CAST(Family.BirthDate AS DATE) AS "生日 (DATE 預設格式)",
CONVERT(VARCHAR(11), Family.BirthDate, 111 ) AS "生日 (yyyy/mm/dd)",
CONCAT(YEAR(Family.BirthDate) , '年' , MONTH(Family.BirthDate) , '月' , DAY(Family.BirthDate), '日') AS "生日(中文)",
FLOOR(DATEDIFF(DAY, Family.BirthDate, GETDATE()) / 365.25) AS "年齡"
FROM Family
SELECT DATEDIFF(now(), Family.BirthDate) AS "年齡"
FROM Family;
年齡 |
---|
4362 |
6493 |
5974 |
12490 |
9006 |
15569 |
SELECT DATEDIFF(now(), Family.BirthDate) / 365.25 AS "年齡"
FROM Family;
年齡 |
---|
11.948 |
17.7823 |
16.3614 |
34.2012 |
24.6626 |
42.6311 |
Query #1
SELECT FLOOR(DATEDIFF(now(), Family.BirthDate) / 365.25) AS "年齡"
FROM Family;
年齡 |
---|
11 |
17 |
16 |
34 |
24 |
42 |
對應範例SQL:
(YEAR(Family.BirthDate) + '年' + MONTH(Family.BirthDate) + '月' + DAY(Family.BirthDate)) + '日' AS "生日(中文)",
FLOOR(DATEDIFF(DAY, Family.BirthDate, GETDATE()) / 365.25) AS "年齡"
每個資料庫系統使用的函式都不太一樣
TO_DATE('2000-01-02', 'YYYY-MM-DD') - TO_DATE('2000-01-01', 'YYYY-MM-DD')
對應範例SQL:
FLOOR(DATEDIFF(DAY, Family.BirthDate, GETDATE()) / 365.25) AS "年齡"
對應範例SQL:
CAST(Family.BirthDate AS DATE) AS "生日 (DATE 預設格式)",
CONVERT(VARCHAR(11), Family.BirthDate, 111 ) AS "生日 (yyyy/mm/dd)",
(YEAR(Family.BirthDate) + '年' + MONTH(Family.BirthDate) + '月' + DAY(Family.BirthDate)) + '日' AS "生日(中文)"
MySQL 超新手入門(4)運算式與函式
http://www.codedata.com.tw/database/mysql-tutorial-4-expression-function/
https://www.w3schools.com/sql/trymysql.asp?filename=trysql_func_mysql_datediff
https://www.w3schools.com/sql/trymysql.asp?filename=trysql_func_mysql_datediff
員工資料表 employees
列出所有員工的「完整姓名」、「出生年月日」,並計算每個人活到2222-11-22的年齡歲數(實歲)