试图加入3个mysql表但我没有得到预期的结果。什么是错误?

时间:2016-05-26 18:31:04

标签: php mysql

以下mysql查询

SELECT *
FROM alerts_list l, alerts_data d, alerts_push_data p
WHERE p.push_data_hash = d.alerts_data_hash
AND p.push_data_alert_id = l.alerts_id
AND d.alerts_data_id = l.alerts_id
AND d.alerts_data_hash =  'JiaYRSVNZxgE'

通过连接三个表来显示JiaYRSVNZxgE的结果。

以下是我使用的表格以及我想要在它们之间连接的列:

表alerts_list

专栏:alerts_id

enter image description here

表alerts_push_data

专栏:push_data_alert_id

专栏:push_data_hash

enter image description here

表alerts_data

专栏:alerts_data_id

专栏:alerts_data_hash

enter image description here

我想要达到的目标是:

push_data_alert_idalerts_id

联系起来

alerts_data_idalerts_id

联系起来

但仅显示alerts_data_hashpush_data_hash"abcdef"的结果

不幸的是,我的查询结果没有找到结果,但实际上有结果。 我做错了什么?

1 个答案:

答案 0 :(得分:2)

您可以使用MySQL JOINS来轻松地执行该操作:

         <?php
            // USING NORMAL JOIN:
            // WE NOW ADD AN EXTRA LAYER (THE VARIABLE $fldVal) 
            // IN THE CASE THAT YOUR VALUE ('JiaYRSVNZxgE') IS DYNAMIC...
            $fldVal = 'JiaYRSVNZxgE';
            $sql    = "SELECT DISTINCT *
                            FROM alerts_list l 
                            JOIN alerts_data d ON d.alerts_data_id=l.alerts_id
                            JOIN alerts_push_data p ON p.push_data_alert_id=l.alerts_id
                            WHERE d.alerts_data_hash='" . $fldVal . "'";



            // USING LEFT JOIN:
            // WE NOW ADD AN EXTRA LAYER (THE VARIABLE $fldVal) 
            // IN THE CASE THAT YOUR VALUE ('JiaYRSVNZxgE') IS DYNAMIC...
            $fldVal = 'JiaYRSVNZxgE';
            $sql    = "SELECT DISTINCT *
                            FROM alerts_list l 
                            LEFT JOIN alerts_data d ON d.alerts_data_id=l.alerts_id
                            LEFT JOIN alerts_push_data p ON p.push_data_alert_id=l.alerts_id
                            WHERE d.alerts_data_hash='" . $fldVal . "'";



            // USING INNER JOIN:
            // WE NOW ADD AN EXTRA LAYER (THE VARIABLE $fldVal) 
            // IN THE CASE THAT YOUR VALUE ('JiaYRSVNZxgE') IS DYNAMIC...
            $fldVal = 'JiaYRSVNZxgE';
            $sql    = "SELECT DISTINCT *
                            FROM alerts_list l 
                            INNER JOIN alerts_data d ON d.alerts_data_id=l.alerts_id
                            INNER JOIN alerts_push_data p ON p.push_data_alert_id=l.alerts_id
                            WHERE d.alerts_data_hash='" . $fldVal . "'";

::并按照CLAUSE ::

进行新的更新
        <?php
            // WE NOW ADD AN EXTRA LAYER (THE VARIABLE $fldVal) 
            // IN THE CASE THAT YOUR VALUE ('JiaYRSVNZxgE') IS DYNAMIC...
            $fldVal = 'JiaYRSVNZxgE';



            $sql2    = "SELECT DISTINCT *
                            FROM alerts_list AS A_LIST
                            LEFT JOIN alerts_push_data A_PUSH ON A_PUSH.push_data_alert_id=A_LIST.alerts_id
                            LEFT JOIN alerts_data A_DATA ON A_DATA.alerts_data_hash=A_PUSH.push_data_hash
                            WHERE A_DATA.alerts_data_hash='" . $fldVal . "'
                            GROUP BY A_LIST.alerts_id";

TEST-CASE QUERY

                    SELECT DISTINCT *
                            FROM alerts_list AS A_LIST
                            LEFT JOIN alerts_push_data A_PUSH ON A_PUSH.push_data_alert_id=A_LIST.alerts_id
                            LEFT JOIN alerts_data A_DATA ON A_DATA.alerts_data_hash=A_PUSH.push_data_hash
                            WHERE A_DATA.alerts_data_hash='iSg2loGJDaWs'
                            GROUP BY A_LIST.alerts_id

<强> RESULT

这些是可以预料的,因为除了 alerts_list 有10行之外,我在所有其他表中只模拟了2行。

转储上方查询的结果 var_dump using the Query Above

表:alerts_list

Table: alerts_list

表:alerts_data Table: alerts_data

表:alerts_push_data Table: alerts_push_data