Mysql比较2个表

时间:2015-05-11 12:58:19

标签: mysql

如何比较不同表格中的三个值?

    pm_meta
    +----+----------+--------+----------+
    | id | item_id  |meta_key|meta_value|
    +----+----------+--------+----------+
    |  2 |     100  | google |googl.com |
    |  3 |     101  | yahoo  |yahoo.com |
    |  3 |     102  | msn    |msn.com   |
    +----+----------+--------+----------+

    pm_embed_code
    +------+-----------+------------+
    | id   | uniq_id   |embed_code  |
    +------+-----------+------------+
    |  100 |     abcd  |<iframe src=|
    |  101 |     dddc  |<iframe src=|
    |  102 |     ffdd  |<iframe src=|
    +----+----------+------------+



    $sql = "SELECT * 
            FROM pm_embed_code, pm_meta
            WHERE pm_embed_code.uniq_id = 'abcd'
            AND pm_embed_code.id = pm_meta.item_id
            AND pm_meta.meta_key = 'google' ";

    echo $row['meta_value']; // I want result "googl.com"

我想查看pm_embed_code.uniq_id= abcd的位置,然后选择pm_embed_code.id然后检查pm_embed_code.id = pm_meta.item_id的位置以及pm_meta.meta_key = google是否全部打印meta_value

更新:

 $sql = "SELECT * 
            FROM pm_meta
            WHERE pm_meta.meta_key = 'google' ";

echo $row['meta_value'];

这个也没有显示任何......为什么?

4 个答案:

答案 0 :(得分:0)

使用join:

Select * from  pm_embed_code join pm_meta on pm_emped_code.id=m_meta.item_id
where pm_embed_code.uniq_id = 'abcd' AND  pm_meta.meta_key = 'google'

答案 1 :(得分:0)

        SELECT pm_embed_code.* , pm_meta.*
        FROM pm_embed_code
        INNER JOIN pm_meta
        ON pm_embed_code.id = pm_meta.item_id
          AND pm_meta.meta_key = 'google' 
        WHERE pm_embed_code.uniq_id = 'abcd'

答案 2 :(得分:0)

您收到了什么错误消息。查询看起来是正确的。

$sql = "SELECT pm.meta_value 
        FROM pm_embed_code pec, pm_meta pm
        WHERE pec.id = pm.item_id
        AND pec.uniq_id = 'abcd'
        AND pm.meta_key = 'google' ";

echo $row['meta_value'];

答案 3 :(得分:0)

查询:

选择pmc.id为&#34; pm_embed_code.id&#34;,pm.meta_key AS&#34; pm_meta.meta_key&#34; FROM pm_meta pm LEFT OUTER JOIN pm_embed_code pmc ON pmc.id = pm.item_id WHERE pmc.uniq_id =&#34; abcd&#34; AND pm.meta_key =&#39; google&#39;

代码

$ sql =&#34; SELECT pmc.id as&#34; pm_embed_code.id&#34;,pm.meta_key AS&#34; pm_meta.meta_key&#34; FROM pm_meta pm LEFT OUTER JOIN pm_embed_code pmc ON pmc.id = pm.item_id WHERE pmc.uniq_id =&#34; abcd&#34; AND pm.meta_key =&#39; google&#39;;&#34;

echo $ row [&#34; pm_meta.meta_key&#34;];