简体   繁体   English

SQL基于列值的选择查询

[英]Sql select query based on a column value

I have a Table1 like this: 我有一个像这样的Table1

ApplicableTo IdApplicable
---------------------------
Dept              1
Grade             3
section           1
Designation       2

There other tables like: 还有其他表,例如:

tblDept : tblDept

ID     Name
1       dept1
2       baking
3       other

tblGrade : tblGrade

ID      Name
1       Grd1
2       Manager
3       gr3

tblSection : tblSection

id      Name
1       Sec1
2       sec2
3       sec3

tblDesignation : tblDesignation

id      Name
1       Executive
2       Developer
3       desig3

What I need is a query for table1 in such a way that gives me 我需要以这种方式查询table1

ApplicableTo (table1)
Name (from the relevant table based on the value in `ApplicableTo` column)

Is this possible? 这可能吗?

Desired Result: 所需结果:

eg: ApplicableTo IdApplicable Name
      Dept            1       dept1
      grade           3       gr3
      Section         1       sec1
      Designation     2       Developer.

This is the result I desire. 这是我想要的结果。

You could do something like the following so the applicable to becomes part of the JOIN predicate: 您可以执行以下操作,使适用于成为JOIN谓词的一部分:

SELECT  t1.ApplicableTo, t1.IdApplicable, n.Name
FROM    Table1 AS t1
        INNER JOIN
        (   SELECT  ID, Name, 'Dept' AS ApplicableTo
            FROM    tblDept
            UNION ALL
            SELECT  ID, Name, 'Grade' AS ApplicableTo
            FROM    tblGrade
            UNION ALL
            SELECT  ID, Name, 'section' AS ApplicableTo
            FROM    tblSection
            UNION ALL
            SELECT  ID, Name, 'Designation' AS ApplicableTo
            FROM    tblDesignation
        ) AS n
            ON n.ID = t1.IdApplicable 
            AND n.ApplicableTo = t1.ApplicableTo

I would generally advise against this approach, although it may seem like a more consice approach, you would be better having 4 separate nullable columns in your table: 我通常建议不要使用这种方法,尽管它看起来似乎更实用,但是最好在表中包含4个单独的可空列:

ApplicableTo | IdDept | IdGrade | IdSection | IdDesignation
-------------+--------+---------+-----------+---------------
Dept         |    1   |  NULL   |    NULL   |     NULL
Grade        |  NULL  |    3    |    NULL   |     NULL
section      |  NULL  |   NULL  |      1    |     NULL
Designation  |  NULL  |   NULL  |    NULL   |       2

This allows you to use foreign keys to manage your referential integrity properly. 这使您可以使用外键来正确管理参照完整性。

The easiest way to achieve that is to add an extra column in table1 to keep the table where id is refferred to. 最简单的方法是在table1中添加一个额外的列,以保留id引用到的表。 Otherwise you can't know in which table the applicable id is reffered to. 否则,您将不知道适用的ID在哪个表中。

Or you can create the applicable id in a way that you can extract the table afterwords from it for example a1 for id 1 in tblDept. 或者,您可以通过以下方式创建适用的ID:可以从中提取表后缀,例如tblDept中ID为1的a1。 And then use [case] ( http://dev.mysql.com/doc/refman/5.0/en/case.html ) (for mysql) in order to make the correct Join. 然后使用[case]( http://dev.mysql.com/doc/refman/5.0/en/case.html )(对于mysql)进行正确的Join。

You can use CASE here, 您可以在这里使用CASE,

SELECT ApplicableTo,
       IdApplicable,
       CASE 
         WHEN ApplicableTo = 'Dept' THEN (SELECT Name FROM tblDept WHERE tblDept.ID = IdApplicable) 
         WHEN ApplicableTo = 'Grade' THEN (SELECT Name FROM tblGrade WHERE tblGrade.ID = IdApplicable)
         WHEN ApplicableTo = 'Section' THEN (SELECT Name FROM tblSection WHERE tblSection.ID = IdApplicable)
         WHEN ApplicableTo = 'Designation' THEN (SELECT Name FROM tblDesignation WHERE tblDesignation.ID = IdApplicable)
       END AS 'Name'
FROM Table1

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

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