简体   繁体   English

如何编写SQL查询?

[英]How to write the sql query?

The table i have is: 我有的表是:

lecturer
  lec_name
  s_code
  lec_pass
  ph_no

proposal_info
  metric,stu_name
  p_title
  u_proposal

apply_supervisor
  metric
  lec1
  lec2
  lec3

Now I would like to join this these 3 table tables and display all the data in the table of proposal_info only for that particular lecturer who login.How login. 现在,我想将这3个表表联接起来,并仅针对登录的特定讲师显示proposal_info表中的所有数据。如何登录。 How to write the sql query for it? 如何编写它的SQL查询? This is the query that i I have written... 这是我写的查询...

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;

I didn't understand exctally what you require, but from whatever i understood, i have created this one. 我不十分了解您的要求,但是根据我的理解,我已经创建了这个。

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

Check if this can help you. 检查这是否可以帮助您。

Where @s_code is the input parameter that you must replace with value. 其中@s_code是必须用值替换的输入参数。

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'
                        )
                )

In comments, you mentioned that you are using MySQL. 在评论中,您提到您正在使用MySQL。 Given that MySQL is not great at handling correlated subqueries. 鉴于MySQL在处理相关子查询方面并不出色。 However, you can easily rewrite the above query like so: 但是,您可以像这样轻松地重写上面的查询:

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'
                    )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM