在Sql中,我使用join从多个表中选择数据。我想要选择具有多个条件的地方,因此我正在使用和,或。 在这里我需要如果jobCode = 1然后选择所有数据,其中jobcode是1,如果JobCode是2,则显示所有数据,其中jobcode是2.But错误是 当我把JobCode = 1时,它所有的数据,2个相同的数据,0个相同的数据。我犯错的地方。帮助我。
USE [Recruitment]
GO
/****** Object: StoredProcedure [dbo].[sp_AdvanceSearch] Script Date: 05/11/2011 09:47:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[sp_AdvanceSearch]
@callval int=9,
@JobCode int=0,
@location_id int=0,
@Position varchar(20)='',
@flag char(1)='',
@JobDesc varchar(MAX)='',
@PostStreamId varchar(10)='',
@GraduQuali varchar(100)='',
@Ten2StreamId varchar(10)='',
@StreamId varchar(10)='',
@ReqExp varchar(15)='',
@Salary varchar(25)='',
@JobLocation varchar(15)='',
@PostDate datetime='',
@EndDate datetime='',
@c_f_name varchar(15)='',
@c_m_name varchar(15)='',
@c_l_name varchar(15)='',
@email varchar(30)='',
@personal_id int=0,
@academic_id int=0,
@family_id int=0,
@profess_id int=0,
@applyId int=0,
@dtInterview int=0,
@TimeReq int=0,
@ExpSalary decimal(12,2)=0,
@EpSalary decimal(12,2)=50000000,
@Resume varchar(max)='',
@howdid int=0,
@dob datetime='1/1/1900',
@do datetime='1/1/2050',
@applydate datetime='',
@prof_qual varchar(10)='',
@prof_stream varchar(50)='0',
@prof_stream1 varchar(24)='',
@prof_stream2 varchar(24)='',
@prof_stream3 varchar(24)='',
@prof_year char(4)='2050',
@grad_qual varchar(10)='Bsc',
@grad_stream varchar(15)='0',
@grad_stream1 varchar(15)='',
@grad_stream2 varchar(15)='',
@grad_stream3 varchar(15)='',
@grad_year char(4)='2050',
@prof_year1 char(4)='0',
@grad_year1 char(4)='0',
@assignment1 int=0,
@assignment2 int=0,
@assignment3 int=0,
@assignment4 int=0,
@assignment5 int=0,
@ass1 int=6,
@ass2 int=6,
@ass3 int=6,
@ass4 int=6,
@ass5 int=6,
@ex_yyyy int=0,
@e_yyyy int=100,
@em_gross decimal(12,2)=50000000,
@emp_gross decimal(12,2)=0,
@city_id int=0,
@city_name varchar(15)=''
as
begin
if @callval=9
begin
declare @proce varchar(5000)
set @proce ='select rp_tbl_personal_details.c_f_name +''''+c_m_name +''''+ c_l_name as name,
rp_tbl_personal_details.dob,rp_tbl_personal_details.email,
rp_tbl_academic_details.prof_qual,rp_tbl_academic_details.grad_qual,
rp_tbl_academic_details.prof_stream,rp_tbl_academic_details.prof_year,
rp_tbl_academic_details.grad_stream,rp_tbl_academic_details.grad_year,
rp_tbl_professional.ex_yyyy,
rp_tbl_family.assignment1,rp_tbl_family.assignment2,rp_tbl_family.assignment3,
rp_tbl_family.assignment4,rp_tbl_family.assignment5,
rp_tbl_professional.emp_gross,rp_tbl_CreateNewJob.JobCode,rp_tbl_CreateNewJob.Position,
rp_tbl_ApplyforJob.personal_id,rp_tbl_ApplyforJob.TimeReq,rp_tbl_ApplyforJob.ExpSalary,
dtInterview = case rp_tbl_ApplyforJob.dtInterview
when 1 then ''Any Day''
when 2 then ''Week Day''
when 3 then ''Weekends''
else ''Other''
end
from rp_tbl_CreateNewJob
inner join rp_tbl_ApplyforJob on
rp_tbl_CreateNewJob.JobCode=rp_tbl_ApplyforJob.JobCode
inner join rp_tbl_personal_details on
rp_tbl_ApplyforJob.personal_id=rp_tbl_personal_details.personal_id
inner join rp_tbl_academic_details on
rp_tbl_personal_details.personal_id=rp_tbl_academic_details.personal_id
inner join rp_tbl_family on
rp_tbl_academic_details.academic_id=rp_tbl_family.academic_id
inner join rp_tbl_professional on
rp_tbl_family.family_id=rp_tbl_professional.family_id where 1=1 and rp_tbl_CreateNewJob.JobCode =' + convert (varchar(50),@JobCode)+ ' and rp_tbl_personal_details.dob BETWEEN ' + ''''+ convert(varchar(50),@dob) + ''''+' and ' +''''+ convert(varchar(50),@do)+''''
if (@prof_qual!='')
begin
set @proce =@proce + ' and rp_tbl_academic_details.prof_qual in ('+'''' + convert (varchar (50),@prof_qual)+''')'
end
if (@prof_stream!='0')
begin
set @proce =@proce + ' and rp_tbl_academic_details.prof_stream in (' + @prof_stream +')'
end
set @proce =@proce + ' and rp_tbl_academic_details.prof_year BETWEEN ' + ''''+ convert(varchar(50),@prof_year1 ) + '''' + ' And ' + '''' + convert(varchar(50),@prof_year )+ ''''
if (@grad_qual!='')
begin
set @proce =@proce + ' and rp_tbl_academic_details.grad_qual in (''' + @grad_qual +''')'
end
if (@grad_stream!='0')
begin
set @proce =@proce + ' and rp_tbl_academic_details.grad_stream in (' + @grad_stream +')'
end
set @proce =@proce + ' or rp_tbl_academic_details.grad_year BETWEEN ' + ''''+ convert(varchar(50),@grad_year1 ) + '''' + ' And ' + '''' + convert(varchar(50),@grad_year )+ ''''
if (@assignment1 != 0)
begin
set @proce =@proce + ' or rp_tbl_family.assignment1 = ' + convert(varchar(50),@assignment1 )
end
if (@assignment2 != 0)
begin
set @proce =@proce + ' or rp_tbl_family.assignment2 = ' + convert(varchar(50),@assignment2 )
end
if (@assignment3 != 0)
begin
set @proce =@proce + ' or rp_tbl_family.assignment3 = ' + convert(varchar(50),@assignment3 )
end
if (@assignment4 != 0)
begin
set @proce =@proce + ' or rp_tbl_family.assignment4 = ' + convert(varchar(50),@assignment4 )
end
if (@assignment5 != 0)
begin
set @proce =@proce + ' or rp_tbl_family.assignment5 = ' + convert(varchar(50),@assignment5 )
end
if (@ex_yyyy !=0 and @e_yyyy!=100)
begin
set @proce =@proce + ' and rp_tbl_professional.ex_yyyy BETWEEN ' + ''''+ convert(varchar(50),@ex_yyyy ) + '''' + ' And ' + '''' + convert(varchar(50),@e_yyyy )+ ''''
end
if (@emp_gross !=0 and @em_gross!=50000000)
begin
set @proce =@proce + ' or rp_tbl_professional.emp_gross BETWEEN ' + ''''+ convert(varchar(50),@emp_gross ) + '''' + ' And ' + '''' + convert(varchar(50),@em_gross )+ ''''
end
if (@ExpSalary !=0 and @EpSalary !=50000000)
begin
set @proce =@proce + ' or rp_tbl_ApplyforJob.ExpSalary BETWEEN ' + ''''+ convert(varchar(50),@ExpSalary ) + '''' + ' And ' + '''' + convert(varchar(50),@EpSalary )+ ''''
end
print (@proce)
exec(@proce)
end
end
答案 0 :(得分:1)
我实际上通过获取输出查询来测试您的代码,这就是我得到的。我省略了where语句之外的其他语句,并为你的表创建了一个别名。
WHERE 1=1
AND cnj.JobCode =0
AND pd.dob BETWEEN 'Jan 1 1900 12:00AM' AND 'Jan 1 2050 12:00AM'
AND ad.prof_qual IN ('')
AND ad.prof_stream IN (0)
AND ad.prof_year BETWEEN '0 ' AND '2050'
AND ad.grad_qual IN ('Bsc')
AND ad.grad_stream IN (0)
OR ad.grad_year BETWEEN '0 ' AND '2050'
OR fa.assignment1 = 0
OR fa.assignment2 = 0
OR fa.assignment3 = 0
OR fa.assignment4 = 0
OR fa.assignment5 = 0
AND pr.ex_yyyy BETWEEN '0' AND '100'
OR pr.emp_gross BETWEEN '0' AND '50000000'
OR apr.ExpSalary BETWEEN '0' AND '50000000'
从我的观察中,我不确定你为什么要包括'1 = 1'。这总是会变成现实。你获得所有/相同数据的原因可能是因为OR语句。您可能需要将OR语句分组在括号内。
AND (
ad.grad_year BETWEEN '0 ' AND '2050'
OR fa.assignment1 = 0
OR fa.assignment2 = 0
OR fa.assignment3 = 0
OR fa.assignment4 = 0
OR fa.assignment5 = 0
)
AND pr.ex_yyyy BETWEEN '0' AND '100'
AND (
pr.emp_gross BETWEEN '0' AND '50000000'
OR apr.ExpSalary BETWEEN '0' AND '50000000')
还要注意BETWEEEN语句中变量输出的空格,您可能还需要处理它。