Mysql之细思极恐(语法问题版)

tech2022-07-07  226

#Mysql数据库 ##表结构:

CREATE TABLE `easybuy_user` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键', `loginName` varchar(255) NOT NULL COMMENT '登录名', `userName` varchar(255) NOT NULL COMMENT '用户名', `password` varchar(255) NOT NULL COMMENT '密码', `sex` int(2) NOT NULL DEFAULT '1' COMMENT '性别(1:男 0:女)', `identityCode` varchar(60) DEFAULT NULL COMMENT '身份证号', `email` varchar(80) DEFAULT NULL COMMENT '邮箱', `mobile` varchar(11) DEFAULT NULL COMMENT '手机', `type` int(2) DEFAULT '0' COMMENT '类型(1:后台 0:前台)' ) CREATE TABLE `easybuy_user_address` ( `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', `userId` int(255) DEFAULT NULL COMMENT '用户主键', `address` varchar(255) DEFAULT NULL COMMENT '地址', `createTime` datetime DEFAULT NULL COMMENT '创建时间', `isDefault` int(2) DEFAULT '0' COMMENT '是否是默认地址(1:是 0否)', `remark` varchar(255) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`id`) )

###1,首先关注easybuy_user表,查询后端用户且性别为1的有哪些? (0:前端用户,1:普通用户)

select * from easybuy_user where type=1 and sex=1

###2, 关注:easybuy_user_address表,查询家住在海淀区且 注册时间为空的用户信息(考察的是模糊查询及NULL值查询)

select * from easybuy_user_address where address like '%海淀区%' and createTime is null

###3,关注:easybuy_user_address表,每个用户系统允许添加多个住址,请查询出不

止一个住址的用户,显示(用户的userName,address)–考察分组及分组过滤

select userId,count(userId) 地址数 from easybuy_user_address group by userId having 地址数>1

###4,关注:easybuy_user_address表,查询哪些用户没有设定默认地址。(2分)(userId)–考察分组及分组过滤

select * from easybuy_user_address where isDefault <>1 select distinct userId from easybuy_user_address where userid not in (select userId from easybuy_user_address where isDefault=1) select userId,sum(isDefault) as mysum from easybuy_user_address group by userId having mysum=0 select userId from easybuy_user_address group by userId having sum(isDefault)=0

###5,关注:easybuy_user 表,查询用户所有女生信息(0:女,1:男),要求显示 :

Id,userName,性别(中文显示 ) --考察Case语句 select id,username, case sex when 0 then '女' when 1 then '男' else '不详' end as '性别' from easybuy_user where sex=0

###6、easybuy_user 表中有帐号(loginName)和密码(password),现在要求使用帐号密码进行登录,用给定的帐号密码判定用户是否登录成功

select count(*) from loginName='admin' and password='e10adc3949ba59abbe56e057f20f883e'

###7、查询产品表(easybuy_product)中单价(price)最高的产品以及库存量最大的产品 并显示出来,显示的效果要求如下:(concat) 产品编号 产品名称 最高单价/最大库存 1001 xxxx 最高单价:1008 1003 xxx 最大库存:983

(select id as 产品编号, name as 产品名称,concat('最高单价:',price) as `最高单价/最大库存` from easybuy_product order by price desc limit 1) union all (select id as 产品编号, name as 产品名称,concat('最大库存:',stock) as `最高单价/最大库存` from easybuy_product order by stock desc limit 1) select id as 产品编号, name as 产品名称,concat('最高单价:',price) as `最高单价/最大库存` from easybuy_product where price=(select max(price) from easybuy_product) union select id as 产品编号, name as 产品名称,concat('最大库存:',stock) as `最高单价/最大库存` from easybuy_product where stock=(select max(stock) from easybuy_product)

###8、查示easybuy_user表中所有用户,要求显示:id,loginName,userName,typeName(显示中文)(关联easybuy_type)–

select a.id,a.loginname,a.username, b.name as typename from easybuy_user a,easybuy_usertype b where a.type=b.id

###9、查询easybuy_user表中哪些用户没有注册地址的?要求显示的是: userName,loginName (easybuy_user(id),easybuy_user_address(userid))(2分)

select a.id, a.loginName, a.userName, b.address from easybuy_user a,easybuy_user_address b where a.id=b.userId and b.address is null

###10,显示出销售业务前1名的销售人员的名单(userName)以及销售额显示出来(5分) userName,销售总额

select userName,sum(订单总金额) as 销售总额 from (select c.userName,b.* from easybuy_order a inner join ( select orderid,sum(quantity*cost) as 订单总金额 from easybuy_order_detail group by orderid) b on a.id=b.orderid inner join easybuy_user c on a.userid=c.id) t group by userName order by 销售总额 desc limit 1 或: select c.username,sum(b.cost*b.quantity) 销售总额 from easybuy_order a INNER JOIN easybuy_order_detail b on a.id=b.orderid INNER JOIN easybuy_user c on a.userid=c.id GROUP BY userid ORDER BY 销售总额 desc limit
最新回复(0)