如何编写sql查询?

时间:2011-03-25 04:24:51

标签: mysql sql

我的表是:

lecturer
  lec_name
  s_code
  lec_pass
  ph_no

proposal_info
  metric,stu_name
  p_title
  u_proposal

apply_supervisor
  metric
  lec1
  lec2
  lec3

现在我想加入这3个表格表,只显示那个登录的特定讲师的proposal_info表格中的所有数据。如何登录。如何为它编写sql查询?这是我写的查询...

select p.metric,p.stu_name,p.p_title,p.u_proposal 
from proposal_info p 
    INNER JOIN apply_supervisor a 
    ON p.metric = a.metric 
        JOIN lecturer s 
        ON a.lec1 = s.s_code 
        or a.lec2 = s.s_code 
        or a.lec3 = s.s_code 
where s.s_code = s_code;

2 个答案:

答案 0 :(得分:0)

我不明白你需要什么,但从我理解的任何东西,我创造了这个。

select p.metric,p.stu_name,p.p_title,p.u_proposal from proposal_info p INNER JOIN
apply_supervisor a ON p.metric = a.metric where 
lec1 = @s_code or 
lec2 = @s_code or 
lec3 = @s_code

检查这是否对您有帮助。

其中@s_code是必须用值替换的输入参数。

答案 1 :(得分:0)

Select P.metric, P.stu_name, P.p_title, P.u_proposal
From proposal_info As P
Where Exists    (
                Select 1
                From apply_supervisor As A
                Where A.metric = P.metric
                    And (
                        A.lec1 = 'some lecturer code'
                        Or A.lec2 = 'some lecturer code'
                        Or A.lec3 = 'some lecturer code'
                        )
                )

在评论中,您提到您正在使用MySQL。鉴于MySQL在处理相关子查询方面不是很出色。但是,您可以轻松地重写上述查询:

Select P.metric, P.stu_name, P.p_title, P.u_proposal
From proposal_info As P
Where P.metric In   (
                    Select A.metric
                    From apply_supervisor As A
                    Where A.lec1 = 'some lecturer code'
                        Or A.lec2 = 'some lecturer code'
                        Or A.lec3 = 'some lecturer code'
                    )