相关推荐recommended
有关MySQL onlinedb数据库的一些查询语句
作者:mmseoamin日期:2023-12-05

(1)查询users表,列出用户的所有信息。

SELECT * FROM users;

(2)查询goods表,列出商品编号、名称和进货量(库存量+销售量,列名为purchases).

SELECT gid, gname, (gquantity + gsale_qty) AS purchases FROM goods;

(3)查询users表,找出2000年以后出生的用户,列出用户名、性别和所在城市。

SELECT uname, ugender, ucity 

FROM users 

WHERE ubirthday >= '2000-01-01';

(4)查询users表,找出使用QQ邮箱的用户,列出登录名、用户名和邮箱地址。

SELECT ulogin,uname, uemail 

FROM users 

WHERE uemail LIKE '%@qq.com';

(5)查询users表,找出来自北京、广州和深圳三个城市的用户,列出用户名、性别和所在城市。

SELECT uname, ugender, ucity 

FROM users 

WHERE ucity IN ('北京', '广州', '深圳');

(6)查询单笔订单金额在5000元以上的订单号。

SELECT oid 

FROM orders 

WHERE oamount >= 5000;

(7)查询users表,列出积分排名前5的用户名和积分。

SELECT uname, ucredit

FROM users 

ORDER BY ucredit DESC 

LIMIT 5;

(8)查询users表,按性别统计用户的平均年龄,列出姓名和平均年龄(avg age)。

SELECT ugender, AVG(DATEDIFF(CURDATE(),ubirthday)/365) AS `avg age`

FROM users

GROUP BY ugender;

(9)查询users表,统计各城市的用户人数(num),并按人数从高到低排序。

SELECT ucity, COUNT(*) AS num

FROM users 

GROUP BY ucity 

ORDER BY num DESC;

(10)查询users表,列出积分排名前5的用户名、积分和名次(ranking).

SELECT uname, ucredit, ranking

FROM (

  SELECT uname, ucredit, RANK() OVER (ORDER BY ucredit DESC) AS ranking

  FROM users

) AS ranked_users

WHERE ranking <= 5;

(11)查询goods表,列出所有乐器类商品的id、名称、类别id和类别名称。

SELECT gid, gname, category.cid AS category_id, category.cname AS category_name

FROM goods

INNER JOIN category ON goods.cid = category.cid

WHERE category.cname = '乐器';

(12)查询图书类商品的总销售量(sale_count)。

SELECT SUM(gsale_qty) AS sale_count

FROM goods

INNER JOIN category ON goods.cid = category.cid

WHERE category.cname = '图书';

(13)查询用户“郭辉”的购物车信息,列出商品id、名称、价格和数量。

SELECT cart.gid, goods.gname, goods.gprice, cart.cnum

FROM cart

INNER JOIN goods ON cart.gid = goods.gid

INNER JOIN users ON cart.uid = users.uid

WHERE users.uname = '郭辉';

(14)查询用户“郭辉”的订单信息,列出订单id、订单编号、订单金额和下单日期。

SELECT orders.oid, orders.ocode, orders.oamount, orders.ordertime

FROM orders

INNER JOIN users ON orders.uid = users.uid

WHERE users.uname = '郭辉';

(15)查询订单号为“O210912082615101”的订单详情,列出商品名称、价格和购买量。

SELECT goods.gname, goods.gprice, ordersitem.inum

FROM ordersitem

INNER JOIN goods ON ordersitem.gid = goods.gid

INNER JOIN orders ON ordersitem.oid = orders.oid

WHERE orders.ocode = 'O210912082615101';

(16)查询购买过“平凡的世界”商品的用户信息,列出用户名、性别和出生日期

SELECT users.uname, users.ugender, users.ubirthday

FROM users

INNER JOIN orders ON users.uid = orders.uid

INNER JOIN ordersitem ON orders.oid = ordersitem.oid

INNER JOIN goods ON ordersitem.gid = goods.gid

WHERE goods.gname = '平凡的世界';

(17)使用联合查询,查询来自北京、广州和深圳3个城市的用户,列出用户名、性别和所在城市。

SELECT uname, ugender, ucity

FROM users

WHERE ucity = '北京'

UNION

SELECT uname, ugender, ucity

FROM users

WHERE ucity = '广州'

UNION

SELECT uname, ugender, ucity

FROM users

WHERE ucity = '深圳';