复杂的sql查询从四个表中获取数据

时间:2014-08-14 06:52:15

标签: mysql sql

我有四张表格如下

表1

的MySQL> select * from PDB_PROFILE;

+-------+------------------------+-------------+-------------------+-----------------------------------------------------------+
| PR_ID | PR_NAME                | PR_PARENTID | PR_TYPE           | PR_KEY                                                    |
+-------+------------------------+-------------+-------------------+-----------------------------------------------------------+
|  1094 | suresh                 |        1077 | service           | serviceproviders::smstest::suresh                         |

表格-2

的MySQL>从MDB_PERMITTED_ADDRESS中选择*;

+---------+------------+
| ADDRESS | SERVICE_ID |
+---------+------------+
| 4444    |       1094 |
+---------+------------+

表3

的MySQL>从MDB_SERVICE_CONN_PR中选择*;

+-------+----------------+-----------------+----------------+--------+------------+---------------------+
| PR_ID | DRIVER_CONN_ID | ALLOCATION_TYPE | EXC_SERVICE_ID | EXT_ID | DEFAULT_OA | SC_ASSIGN_DATE      |
+-------+----------------+-----------------+----------------+--------+------------+---------------------+
|  1161 |           1158 | e               |           1094 |   NULL | NULL       | 2014-08-14 11:28:20 |
+-------+----------------+-----------------+----------------+--------+------------+---------------------+

表4

的MySQL>从MDB_DRIVER_CONN_PR中选择*;

+-------+-----------+-----------+------+--------------+-------------+---------+------------+-----------+--------------------+---------------+---------------------+
| PR_ID | DRIVER_ID | PARENT_ID | TYPE | SHORT_NUMBER | DESCRIPTION | ID_TYPE | DEFAULT_OA | IS_GLOBAL | DEFAULT_SERVICE_ID | CONN_CATEGORY | SC_CREATION_DATE    |
+-------+-----------+-----------+------+--------------+-------------+---------+------------+-----------+--------------------+---------------+---------------------+
|  1158 |      1067 |      NULL | SMS  | 2222*        |             | s       | 2222       | n         |               NULL | Generic       | 2014-08-14 11:28:07 |
+-------+-----------+-----------+------+--------------+-------------+---------+------------+-----------+--------------------+---------------+---------------------+

检查PR_TYPE = service的第一个表然后需要获取相同的PR_ID并从其他三个表中获取数据与PR_ID

最后我需要输出如下

+------------------------+------------+---------+
| PR_NAME                | DEFAULT_OA | ADDRESS |
+------------------------+------------+---------+
| suresh                 | 2222       | 4444    |
+------------------------+------------+---------+

2 个答案:

答案 0 :(得分:1)

我希望这是正确的

SELECT  P.PR_NAME,
        DC.DEFAULT_OA,
        PA.ADDRESS 
FROM    PDB_PROFILE P
JOIN    MDB_PERMITTED_ADDRESS PA ON P.PR_ID = PA.SERVICE_ID
JOIN    MDB_SERVICE_CONN_PR SC ON P.PR_ID = SC.EXC_SERVICE_ID
JOIN    MDB_DRIVER_CONN_PR DC ON SC.DRIVER_CONN_ID = DC.PR_ID

答案 1 :(得分:0)

此查询可以为您提供帮助!

select TAB1.PR_NAME , TAB4.DEFAULT_OA,  TAB2.ADDRESS 
from  PDB_PROFILE TAB1, 
      MDB_PERMITTED_ADDRESS TAB2,
      MDB_SERVICE_CONN_PR TAB3, 
      MDB_DRIVER_CONN_PR TAB4

WHERE TAB1.PR_ID = TAB2.SERVICE_ID
AND TAB1.PR_ID = TAB3.EXC_SERVICE_ID
AND TAB3.DRIVER_CONN_ID = TAB4.PR_ID
AND TAB1.PR_TYPE = 'service'