1、等值连接
select ``` FROM T1,T2 WHERE T1.id=T2.id
2、内连接
select ``` from t1 inner join t2 on t1.id=t1.id where conition
account 表
cart 表
category 表
goods 表
# 1:火腿肠被谁买了?
SELECT goods.goods_name,account.name
FROM account,cart,goods
WHERE goods.good_no = cart.goods_no
AND cart.account_id = account.id
AND goods.goods_name='火腿肠'
# 2:零食被谁买了?
SELECT
account.name,cart.num
FROM
account,cart,category,goods
WHERE
goods.category_no=category.no
AND goods.good_no=cart.goods_no
AND cart.account_id=account.id
AND category.name='零食'
# 3、李四买了哪些商品?(名称,价格,时间,数量````
SELECT goods_name,price,cart.create_time,num
FROM goods,cart,account
WHERE account.id=cart.account_id
AND cart.goods_no=goods.good_no
AND account.name='李四'
# 4:所有用户分别买了多少钱?````
SELECT
account.name,SUM(cart.num * goods.price)
FROM
account,cart,goods
WHERE
account.id=cart.account_id
AND
cart.goods_no=goods.good_no
GROUP BY
account.name
# 5:假设购物车东西全部销售,周几的营业额最高
SELECT SUM((goods.price*cart.num)) AS '营业额', DAYOFWEEK(cart.create_time)
FROM cart,goods
WHERE cart.goods_no=goods.good_no
GROUP BY DATE_FORMAT (cart.create_time,'%Y-%m-%d')# 对每一天编组
ORDER BY SUM((goods.price*cart.num))DESC
LIMIT 0,1
# 6:李四在什么时候购物
SELECT cart.create_time,account.name
FROM account,cart
WHERE account.id=cart.account_id
AND account.name='李四'
# 7:购物车里的商品销售后,赚了多少钱````
SELECT SUM((price*num)-(cost*num)) AS '利润'
FROM goods,cart
WHERE cart.goods_no=goods.good_no
# 8:求哪个商品利润率最高
#### 利润价格=(销售价格-成本价格)/成本价格````
SELECT goods.goods_name,(price-cost)/cost AS '最高利润'
FROM cart,goods
WHERE cart.goods_no=goods.good_no
ORDER BY (price-cost)/cost DESC
LIMIT 0,1
# 9: 求2023年3月12日前一周上架的商品```
SELECT * FROM goods
WHERE DATE_FORMAT(crate_time,'%Y-%m-%d')>DATE_SUB('2023-03-12',INTERVAL 1 WEEK)
AND DATE_FORMAT(crate_time,'%Y-%m-%d')<'2023-03-12'
# 10:补充知识
时间查询
表示一周中的某一天,以数字表示,其中 1 代表星期日,2 代表星期一,依此类推。
DAYOFWEEK
表示一年中的某一天,以数字表示,范围在 1 到 365(或闰年中的366),表示从年初过去的天数。
DAYOFYEAR
表示一个月中的某一天,以数字表示,指示给定月份中的具体日子。
DAYOFMONTH
SELECT NOW( ) # 取日期和时间
SELECT CURRENT_DATE # 取日期
SELECT CURRENT_TIME # 取时间
SELECT DATE_FORMAT( NOW(),'%Y-%m-%e') # 提取 包含时间的日期 中的日期部分,不要时间部分