次の
テーブルが
あり
ます
。
オプション:- 1 つのオプション:結合を使用して学生のすべての詳細を取得するクエリ:-
select s.name, sd.city, sc.className
from student as s join student_address sd on s.addId = sd.addId
inner join student_class sc on sc.classId = s.classId
inner join student_hobby sh on sh.stuId = s.stuId
where sh.hobby REGEXP 'cricket|footbal';
別のオプションは、ストアド関数を使用しています:
select s.name, sd.city, sc.className
from student as s join student_address sd on s.addId = sd.addId
inner join student_class sc on sc.classId = s.classId
where f_searchHobby(s.stuId,'cricket|footbal')=1;
create function f_searchHobby(
sId int,
matches varchar(100)
) returns int
begin
select count(*) into @count from student_hobby where hobby regexp matches and stuId = sId;
if @count > 1 then
return 1 ;
else
return 0;
end if;
end
両方の結果が結果セットを取得していると考えてください。
ですから、重いデータベースに適したアプローチを提案させてください。
ありがとう、