2015-12-02 49 views
0

我有一個名爲tb_customer master的表。簡化子查詢mysql以獲取關係表中的所有列

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等等,而不是像在子查詢中一樣逐一選擇。如果一個接一個地確定它,那麼這個命令就太長了。 任何建議,以便理解

UPDATE

感謝您的快速響應。我之所以使用子查詢是因爲我的報表主表有許多外鍵。 這是完整的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) 

順便說一句,再次感謝。

所以這樣的情況: 還有另一個表。

回答

0

連接是通常速度更快,更簡單,比子查詢:

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" 
; 
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"; 

另一個好處:這是方式更具有可讀性。

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