1、用到MySQL的 FIND_IN_SET() 函数。
2、主表:recruitment_demand,子表:recruitment_jobs
需求:需要查询出主表以及关联的子表中的work_name、和user_num字段,work_name拼接起来用逗号隔开,以及统计user_num的总和。
查询语句:
结果图:
1、创建表create table a (name varchar(20) not null,sex varchar(10))
create table b (name varchar(20) not null,sex varchar(10))
2、插入值
insert into a values('john','male')insert into b values('john','male')
insert into a values('kate','female')insert into b values('kate','female')
3、查询方法1
mysql>select name,sex from a where name in (select name from b where name='john')
+------+------+
| name | sex |
+------+------+
| john | male |
+------+------+
1 row in set (0.00 sec)
4、查询方法2
mysql>select a.name,a.sex from a,b where a.name=b.name and b.name='john'
+------+------+
| name | sex |
+------+------+
| john | male |
+------+------+
1 row in set (0.00 sec)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)