【问题标题】:how to find displaying data where data not exist in the other table如何在另一个表中不存在数据的情况下查找显示数据
【发布时间】:2018-02-11 01:03:56
【问题描述】:

我有 3 张桌子:

table_1:

| id | test |total_employee|
+----+------+--------------+
| 1  | xxxx |       3      |
| 2  | yyyy |       2      |
| 3  | zzzz |       3      |
----------------------------

table_2:

| id | id_table1 |id_employee|
+----+-----------+-----------+
| 1  |    1      |     1     |
| 2  |    1      |     2     |
| 3  |    1      |     3     |
| 4  |    1      |     1     |
| 5  |    1      |     2     |
| 6  |    1      |     1     |
| 7  |    1      |     2     |
| 8  |    1      |     3     |
-----------------------------

表员工:

    | id | name |
    +----+------+
    | 1  | emp1 |
    | 2  | emp2 |
    | 3  | emp3 |
    -------------

现在我想显示表employee和table_2中的所有员工,如果表employee中的员工记录存在但table_2中不存在。如果记录不存在则标记为“未选中”,如果存在则标记为“已选中”。

重点是如何从table_employee中查找数据,无论table_2上是否存在数据。

【问题讨论】:

    标签: php sql


    【解决方案1】:

    IF() 操作让您可以这样做:

    SELECT e.*, IF(ISNULL(t2.id), 'unchecked', 'checked') AS `checked` 
    FROM table_employee e 
    LEFT JOIN table_2 t2 ON t2.id_employee = e.id
    

    【讨论】:

    • 对不起,我改变了我的问题,无论 table_2.d 上是否存在数据,如何从 table_employee 中查找数据
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多