简体   繁体   English

根据另一个表中的行类型选择行

[英]Select rows according row type in another table

This is my database structure. 这是我的数据库结构。

Member Table(Type - S: Student, T: Teacher) 会员表(类型 - S:学生,T:教师)

+---+----+----+----+
|idx|type| id | pw |
+---+----+----+----+
| 1 | S  | A  | .. |
| 2 | S  | B  | .. |
| 3 | T  | C  | .. |
| 4 | T  | D  | .. |
| 5 | S  | E  | .. |
| 6 | S  | F  | .. |
+---+----+----+----+

Student Table 学生表

+---+-----+-----+------+
|idx|grade|class|number|
+---+-----+-----+------+
| 3 |   3 |   8 |   29 |
| 4 |   2 |  10 |   13 |
+---+-----+-----+------+

Teacher Table 老师表

+---+-------+
|idx|enabled|
+---+-------+
| 3 | N     |
| 4 | N     |
+---+-------+

I want to get member info with specific info according to member type. 我想根据会员类型获取具有特定信息的会员信息。

my code is 我的代码是

$result = query("SELECT * FROM `member` WHERE `id` = '...' AND `pw` = '...'");
$member_info = fetch_obj($result);

if ($member_info->type === 'T') {
    $result = query("SELECT * FROM `teacher` WHERE `idx` = '...'");
}
else {
    $result = query("SELECT * FROM `student` WHERE `idx` = '...'");
}
$specific_info = fetch_obj($result);

But, I want to get all data in just one query request. 但是,我想在一个查询请求中获取所有数据。
like: 喜欢:

SET @type = SELECT `type` FROM `member` WHERE `member`.`idx` = '2';

SELECT 
    CASE @type
    WHEN 'S' THEN `member`.*, `student`.*
    WHEN 'T' THEN `member`.*, `teacher`.*
    END
FROM 
    CASE @type
    WHEN 'S' THEN `member`, `student`
    WHEN 'T' THEN `member`, `teacher`
    END
WHERE
    `member`.`idx` = '2' AND
    CASE @type
    WHEN 'S' THEN
        `student`.`idx` = `member`.`idx`
    WHEN 'T' THEN
        `teacher`.`idx` = `member`.`idx`
    END
;

How can I do? 我能怎么做?

If the student and teacher tables have the same columns then the easiest solution is probably to UNION 2 queries together, working on the basis that one of the unioned queries will not return anything. 如果学生和教师表具有相同的列,那么最简单的解决方案可能是UNION 2查询,因为其中一个联合查询不会返回任何内容。

Something like this, although I would specify the columns I wanted returned and not use SELECT * 像这样的东西,虽然我会指定我想要的列返回而不使用SELECT *

SELECT `member`.*, `student`.*
FROM `member`
INNER JOIN `student`
ON `student`.`idx` = `member`.`idx`
WHERE `member`.`idx` = '2' 
AND member.type = 'S'
UNION
SELECT `member`.*, `teacher`.*
FROM `member`
INNER JOIN `teacher`
ON `teacher`.`idx` = `member`.`idx`
WHERE `member`.`idx` = '2' 
AND member.type = 'T'

Since it appears in your data the Idx column in your member table could represent both that of a student AND a teacher, I would do a double left-join. 由于它出现在您的数据中,您的成员表中的Idx列既可以代表学生也可以代表教师,我会进行双左连接。 Something like (and I try to never use select * ) 像(我尝试永远不会使用select *)

select
      m.idx,
      m.type,
      m.id,
      m.pw,
      case when s.idx IS NULL then 0 else 1 end as IsStudent,
      s.grade,
      s.class,
      s.number,
      case when t.idx IS NULL then 0 else 1 end as IsTeacher,
      t.enabled
   from
      Member m
         LEFT JOIN Student s
            on m.idx = s.idx
         LEFT JOIN Teacher t
            on m.idx = t.idx
   where
      m.idx = 2

Then, in your result set, you will have two "flag" (0 or 1) columns IsStudent and IsTeacher so you could conditionally use the other columns IF they are so available. 然后,在结果集中,您将有两个“标志”(0或1)列IsStudent和IsTeacher,因此您可以有条件地使用其他列,如果它们可用的话。

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

相关问题 根据另一个表中ID的代表次数从一个表中选择行 - Select rows from one table according to the number of reps of the id in another 在一个表的插入上创建触发器,根据另一个(第3个)表上的行数在另一个表上创建新行 - Creating a trigger on insert on a table, that creates new rows on another table, according to amount of row on another (3rd) table 从表中选择只连接另一个表的一行的行 - SELECT rows from a table joining only one row of another table 选择一个表的一行,然后选择另一个表的所有行 - SELECT one row of a table, And all rows in another table 从一个表中选择行,其中另一个表中的行具有多个条件 - Select rows from a table where row in another table with multiple condition 根据另一个表中的条件对一个表中的行进行计数 - Count rows in a table according to criteria in another table MySQL查询根据不在另一个表中的特定键从表中选择行 - MySQL query select rows from table according to specific key that is not in another table MySQL选择行与另一个表中的两个匹配的联接行 - MySQL select row with two matching joined rows from another table 选择一个表中的每一行以及另一表中的相关行 - select every row in one table and related rows from another 当另一个表中没有行时如何从MySQL选择行 - How to select a row from MySQL when there are no rows in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM