简化子查询mysql以获取关系表

时间:2015-12-02 20:03:30

标签: mysql

我有一个名为tb_customer master的表。

mysql> select COSTUMER_ID, NAMA, ATTENTION, IN_DATE, IN_REF, JOB_REF, LAST_CARGO FROM tb_customer_master;
+-------------+----------------------+-------------------------+------------+--------+---------+----------------------+
| COSTUMER_ID | NAMA                 | ATTENTION               | IN_DATE    | IN_REF | JOB_REF | LAST_CARGO           |
+-------------+----------------------+-------------------------+------------+--------+---------+----------------------+
|           2 | Eagletainer          | Ms. Joyce Ong Chong Mei | NULL       | 1234   | 123     | Lube                 |
|           5 | APL                  | Test                    | 21-11-2015 | sgdgfa | sgfsd   | FOOD                 |
+-------------+----------------------+-------------------------+------------+--------+---------+----------------------+
2 rows in set (0.00 sec)

我也有表作为表主人,具有作为报告主的行为

mysql> select REPAIR_ESTIMATE_ID, EIR_REF, COSTUMER_ID FROM tb_master_repair_estimate;
       +--------------------+------------+-------------+
       | REPAIR_ESTIMATE_ID | EIR_REF    | COSTUMER_ID |
       +--------------------+------------+-------------+
       |                 38 | 1545053    |           5 |
       |                 40 | 1545052    |           5 |
       |                 41 | 1545054    |           5 |
       +--------------------+------------+-------------+
3 rows in set (0.00 sec)

现在,对于一个案例,我想像他们一样对它们进行子查询

mysql> SELECT
->  a.EIR_REF as EIR,
->
->  (SELECT NAMA FROM tb_customer_master c
->          WHERE a.COSTUMER_ID = c.COSTUMER_ID ) as "Name Of Customer",
->
->  (SELECT ATTENTION FROM tb_customer_master c
->          WHERE a.COSTUMER_ID = c.COSTUMER_ID ) as "ATTENTION"
->
-> FROM tb_master_repair_estimate a
->
-> WHERE a.EIR_REF = "1545052";
+------------+----------------------+-----------+
| EIR        | Name Of Customer     | ATTENTION |
+------------+----------------------+-----------+
| 1545052    | APL                  | Test      |
+------------+----------------------+-----------+
1 row in set (0.00 sec)

我的问题是,我想让我的最后一个问题变得简单。如何使用单列eir,客户名称,注意,in_date,in_ref等简单,而不是在子查询中逐个选择。如果一个一个地确定它是如此长的命令。 任何建议如此赞赏

更新

感谢您的快速反应。我之所以使用子查询是因为我的报表主表有很多外键。 这是完整的tbl_report

mysql> select EIR_REF, NO_TANK, COSTUMER_ID, TANK_ID, TOTAL from   tb_master_repair_estimate where EIR_REF = "1545052";
+------------+---------+-------------+---------+-------+
| EIR_REF    | NO_TANK | COSTUMER_ID | TANK_ID | TOTAL |
+------------+---------+-------------+---------+-------+
| 1545052    |       7 |           5 |       1 |  NULL |
+------------+---------+-------------+---------+-------+

另一个表再次命名为tb_tank_type

mysql> select * from tb_tank_type;
+---------+-----------+------------+
| TANK_ID | NAMA_TYPE | KETERANGAN |
+---------+-----------+------------+
|       1 | IM04      | NULL       |
|       2 | XXXX      | NULL       |
+---------+-----------+------------+
2 rows in set (0.00 sec)

我尝试使用我的代码将它们组合在一起:

mysql> SELECT
->  a.EIR_REF as EIR,
->
->  (SELECT NAMA_TYPE FROM tb_tank_type b
->     WHERE b.TANK_ID = a.TANK_ID) as "type tank",
->
->  (SELECT NAMA FROM tb_customer_master c
->          WHERE a.COSTUMER_ID = c.COSTUMER_ID ) as "Name Of Customer",
->
->  (SELECT ATTENTION FROM tb_customer_master c
->          WHERE a.COSTUMER_ID = c.COSTUMER_ID ) as "ATTENTION",
->
->  (SELECT PREFIX FROM tb_iso_tanks d
->          WHERE a.NO_TANK = d.ID_TANK)    as "PREFIX",
->
->  (SELECT SERIAL_NUMBER FROM tb_iso_tanks d
->          WHERE a.NO_TANK = d.ID_TANK)    as "SERIAL_NUMBER"
->
-> FROM tb_master_repair_estimate a
->
-> WHERE a.EIR_REF = "1545052";
+------------+-----------+----------------------+-----------+--------+---------------+
| EIR        | type tank | Name Of Customer     | ATTENTION | PREFIX | SERIAL_NUMBER |
+------------+-----------+----------------------+-----------+--------+---------------+
| 1545052    | IM04      | APL                  | Test      | EOLU   |       1234567 |
+------------+-----------+----------------------+-----------+--------+---------------+
1 row in set (0.00 sec)
不过,再次感谢。

所以这样的情况: 还有另一张表。

3 个答案:

答案 0 :(得分:0)

JOIN通常比子查询更快,更简单:

SELECT a.EIR_REF as EIR, c.NAMA AS `Name of Customer`, c.ATTENTION AS ATTENTION
FROM tb_master_repair_estimate a
   LEFT JOIN tb_customer_master c USING (COSTUMER_ID)
WHERE a.EIR_REF = "1545052"
;

答案 1 :(得分:0)

如果我理解正确,您可以使用JOIN表格tb_customer_master到您的表格tb_master_repair_estimate来简化您的查询。

从您的陈述中得出的示例:

SELECT
    a.EIR_REF as EIR,
    c.NAMA as "Name Of Customer",
    c.ATTENTION as "ATTENTION"
FROM tb_master_repair_estimate a
INNER JOIN tb_customer_master c ON a.COSTUMER_ID = b.COSTUMER_ID
WHERE a.EIR_REF = "1545052";

另一个好处:这更具可读性。

答案 2 :(得分:0)

看起来你需要在这个例子中使用连接:

select a.EIR_REF, c.NAMA, c.ATTENTION
from tb_master_repair_estimate a
join tb_customer_master c
on c.COSTUMER_ID = a.COSTUMER_ID
where a.EIR_REF = '1545052';