MySQL选择查询以获取列表值的记录

时间:2016-01-01 05:25:57

标签: mysql sql select group-concat find-in-set

我正在使用MySQL。 我有3个表格如下。

表:subject_Master

--------------------------
subjectId   | subjectShortName
----------------------------------
1           |   English
2           |   French
3           |   German
4           |   Latin
----------------------------------

表:class_Master

-----------------------------------
classId     | className
----------------------------------
1           |   Rose
2           |   Dasy
3           |   Lily

表:subjectAllocation

------------------------------------------
allocationId    |   classId |   subjectId
-------------------------------------------
1               |   1       |   1,2
2               |   2       |   2,3,4
3               |   3       |   1,2,3,4

我如何获得以下SQL结果,想要获取subjectAllocation行中每个subjectId的SubjectName

ClassName   |   SubjectName
-------------------------------------------
Rose        |   English,French
Dasy        |   French,German,Latin
Lily        |   English,French,German,Latin

2 个答案:

答案 0 :(得分:4)

使用FIND_IN_SET()功能:

试试这个:

SELECT A.allocationId, 
       B.className, 
       GROUP_CONCAT(C.subjectShortName) AS subjectName
FROM subjectAllocation A
INNER JOIN class_Master B ON A.classId = B.classId 
INNER JOIN subject_Master C ON FIND_IN_SET(C.subjectId, A.subjectId) 
GROUP BY A.allocationId;

答案 1 :(得分:1)

<强>计划

  
      
  • 修复subjectAllocation以便它可以访问&amp;可交联
  •   
  • 使用序列生成器(来自digits_v)将可变长度字符串展开为结构化数据
  •   
  • 以简单的方式访问结构化数据,加入索引字段
  •   

<强>设置

create table subject_Master
(
  subjectId integer primary key not null,
  subjectShortName varchar(23) not null
);

create table class_Master
(
  classId integer primary key not null,
  className varchar(23) not null
);

create table subjectAllocation_inaccessible
(
  allocationId integer primary key not null,
  classId integer not null,
  subjectId varchar(32) not null,
  foreign key ( classId ) references class_Master ( classId )
);

create table subjectAllocation
(
  allocationId integer primary key not null,
  classId integer not null,
  subjectId integer not null,
  foreign key ( classId ) references class_Master ( classId ),
  foreign key ( subjectId ) references subject_Master ( subjectId )
);

insert into subject_Master
( subjectId, subjectShortName )
values
( 1           ,   'English' ),
( 2           ,   'French'  ),
( 3           ,   'German'  ),
( 4           ,   'Latin'   )
;

insert into class_Master
( classId, className )
values
( 1           ,   'Rose' ),
( 2           ,   'Dasy' ),
( 3           ,   'Lily' )
;

insert into subjectAllocation_inaccessible
( allocationId, classId, subjectId )
values
( 1               ,   1       ,   '1,2'      ),
( 2               ,   2       ,   '2,3,4'    ),
( 3               ,   3       ,   '1,2,3,4'  )
;

修复subjectAllocation

create view digits_v
as
SELECT 0 AS N 
UNION ALL 
SELECT 1 
UNION ALL 
SELECT 2 
UNION ALL 
SELECT 3 
UNION ALL 
SELECT 4 
UNION ALL 
SELECT 5 
UNION ALL 
SELECT 6 
UNION ALL 
SELECT 7 
UNION ALL 
SELECT 8 
UNION ALL 
SELECT 9
;

insert into subjectAllocation
( allocationId, classId, subjectId )
select @row_number := @row_number + 1 as allocationId, sa.classId, substring_index(substring_index(sa.subjectId, ',', n.n), ',', -1) sub
from subjectAllocation_inaccessible sa
cross join
(
  select a.N + b.N * 10 + 1 n
  from digits_v a
  cross join digits_v b
  order by n
) n
cross join ( select @row_number := 0 ) params
where n.n <= 1 + (length(sa.subjectId) - length(replace(sa.subjectId, ',', '')))
;

访问简单

select c.className, group_concat(s.subjectShortName)
from subjectAllocation sa
inner join class_Master c
on sa.classId = c.classId
inner join subject_Master s
on sa.subjectId = s.subjectId
group by c.className
;

这里对class_Master的连接可以使用主索引(subjectId)

<强>输出

+-----------+----------------------------------+
| className | group_concat(s.subjectShortName) |
+-----------+----------------------------------+
| Dasy      | French,German,Latin              |
| Lily      | German,Latin,English,French      |
| Rose      | English,French                   |
+-----------+----------------------------------+

<强> sqlfiddle

<强>参考