这3个查询可以组合成一个MySQL查询吗?

时间:2015-06-16 18:39:22

标签: php mysql sql

我有3个表(标签,标签,投资组合)

标签有2个字段 - > tags_id(ai),tags_name

标签有3个字段 - > tag_id(ai),tag_oid​​(portfolio_id),tag_tid(tags_id)

投资组合有很多领域 - > portfolio_id等

我想匹配已标记为' PD-02'的投资组合记录。我已经能够在三个单独的查询中执行此操作,并且一直尝试将它们组合到一个查询中。标签表包含标签名称/标题,然后标签表格与标签名称建立关系到其他表格 - 如投资组合。

$tags = query_select(
    "SELECT * 
    FROM tags
    WHERE tags_name = 'PD-02'");

if (!empty($tags)) {
    $related_portfolios = query_select(
        "SELECT *
        FROM tag
        WHERE tag_tid = $tags[0]['tags_id']"
    );

    if (!empty($related_portfolios)) {
        $portfolios = array();
        foreach ($related_portfolios as $tkey => $tag) {
            $portfolios[] = query_select("SELECT * FROM portfolio WHERE portfolio_id = $tag['tag_oid']");
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您可以尝试<html> <script language="javascript" type="text/javascript"> <!-- Begin function convert(input) { var inputlength = input.length; input = input.toLowerCase(); var phonenumber = ""; for (i = 0; i < inputlength; i++) { var character = input.charAt(i); switch (character) { case '0': phonenumber += "0"; break case 'a': phonenumber += "1 "; break; case 'b': phonenumber += "2 "; break; case 'c': phonenumber += "3 "; break; case 'd': phonenumber += "4 "; break; case 'e': phonenumber += "5 "; break; case 'f': phonenumber += "6 "; break; case 'g': phonenumber += "7 "; break; case 'h': phonenumber += "8 "; break; case 'i': phonenumber += "9 "; break; case 'j': phonenumber += "10 "; break; case 'k': phonenumber += "11 "; break; case 'l': phonenumber += "12 "; break; case 'm': phonenumber += "13 "; break; case 'n': phonenumber += "14 "; break; case 'o': phonenumber += "15 "; break; case 'p': phonenumber += "16 "; break; case 'q': phonenumber += "17 "; break; case 'r': phonenumber += "18 "; break; case 's': phonenumber += "19 "; break; case 't': phonenumber += "20 "; break; case 'u': phonenumber += "21 "; break; case 'v': phonenumber += "22 "; break; case 'w': phonenumber += "23 "; break; case 'x': phonenumber += "24 "; break; case 'y': phonenumber += "25 "; break; case 'z': phonenumber += "26 "; break; case '-': phonenumber += "-"; break; case '1': phonenumber += "a"; break; case '2': phonenumber += "b"; break; case '3': phonenumber += "c"; break; case '4': phonenumber += "d"; break; case '5': phonenumber += "e"; break; case '6': phonenumber += "f"; break; case '7': phonenumber += "g"; break; case '8': phonenumber += "h"; break; case '9': phonenumber += "i"; break; case '1-0': phonenumber += "j"; break; case '1-1': phonenumber += "k"; break; case '1-2': phonenumber += "l"; break; case '1-3': phonenumber += "m"; break; case '1-4': phonenumber += "n"; break; case '1-5': phonenumber += "o"; break; case '1-6': phonenumber += "p"; break; case '1-7': phonenumber += "q"; break; case '1-8': phonenumber += "r"; break; case '1-9': phonenumber += "s"; break; case '2-0': phonenumber += "t"; break; case '2-1': phonenumber += "u"; break; case '2-2': phonenumber += "v"; break; case '2-3': phonenumber += "w"; break; case '2-4': phonenumber += "x"; break; case '2-5': phonenumber += "y"; break; case '2-6': phonenumber += "z"; break; } } document.myform.number.value = phonenumber; return true; } // End --> </script> <form name=myform> <table border=0> <tr> <td>Alphanumeric #:</td> <td> <input type=text size=20 maxlength=20 name=alphabet value="Will123"> </td> </tr> <tr> <td>Converted #:</td> <td> <input type="text" size=20 maxlength=20 name="number"> </td> </tr> <tr> <td align=center colspan=2> <input type=button value="Convert" onClick="return convert(document.myform.alphabet.value)"> </td> </table> </form> </html>

inner join

答案 1 :(得分:0)

此查询可能有效。

SELECT *
FROM tags, tag, portfolio
WHERE tags.tag_name='PD-02'
AND tags.tag_id=tags.tags_id
AND tags.tag_oid=portfolio.portfolio_id;