MYSQL查询将多个字段连接到一个字段

时间:2012-05-29 21:59:58

标签: php mysql join union

我已经尝试过寻找答案了,但是还没有找到一个似乎让我在我想要的地方。我现在有两张桌子,我正试图加入。

adhesive_table

id | adhesive_name | adhesive_code
1  | Ivory         | 0090
2  | Jet Black     | 0100
3  | Khaki         | 0110
4  | Natural       | 0120

和match_table

id | brand_id | sheet_name | match_1 | match_2
1  | 1        | Wheat      | 0110    | 0120
2  | 1        | Dew        | 0090    | 0110

等......有成千上万的火柴和数百种粘合剂。我正在尝试抓住匹配并使用JOIN返回相应的adhesive_name。

$sql = "SELECT * FROM match_table
JOIN adhesive_table
ON match_table.match_1 = adhesive_table.adhesive_code
WHERE match_table.brand_id
IN ($b)";

($ b通过$ .post发送并对应brand_id)

这适用于单场比赛。但对于许多人来说,match_2指示了第二种(替代)匹配。所以我一直试图让JOIN工作,我可以抓住比赛1,2,3的名称无济于事。我一直在尝试查询:

$sql = "SELECT * FROM match_table
LEFT JOIN adhesive_table
ON match_table.match_1 = adhesive_table.adhesive_code
LEFT JOIN adhesive_table
ON match_table.match_2 = adhesive_table.adhesive_code
WHERE match_table.brand_id
IN ($b)";

$sql = "SELECT * FROM match_table
JOIN adhesive_table
ON match_table.match_1 = adhesive_table.adhesive_code
AND match_table.match_2 = adhesive_table.adhesive_code
WHERE match_table.brand_id
IN ($b)";

但是都不行。我想知道我的表的格式是否阻止了它的工作,或者我是否遗漏了一些明显的东西。这是否需要使用group_concat?

感谢您提供的任何帮助。

我应该添加我希望获得的表格,结果如下:

brand_id = 1
sheet_name = Wheat
 match_1 = (adhesive_code & adhesive_name) 0110 Khaki
 match_2 = (adhesive_code & adhesive_name) 0120 Natural

查询后,结果将放入数组

$arr = array();
$result = mysql_query($sql);
if($result) {
    while($row = mysql_fetch_array($result)){
        $arr[] = $row;
} 
} else {
$arr[] = "";
}
echo json_encode($arr);

这会被发送回我的主文件,我将结果显示在一个循环中,如下所示:

$.post("getmatches.php",{checkedBrand:checkedBrand},function(json){
    for(var i=0; i<json.length; i++){
        htmlFormat += 
        " sheet : " + json[i].sheet_name
        + " - " + "match_1 : " + json[i].adc1 
        + " - " + "name : "  + json[i].ad1
        + " - " + "match_2 : " + json[i].adc2
        + " - " + "name : "  + json[i].ad2
        + "<br />";
    } // end for loop
    $(".stuffs").html(htmlFormat)
}) // end post  

非常感谢Sebas的耐心和帮助,使用的最终工作方法如下。我添加了选择AS别名列,所以我可以在json返回后在javascript中引用它们。

$sql="
SELECT brand_id, sheet_name, a.adhesive_name AS ad1, a.adhesive_code AS adc1, b.adhesive_name ad2, b.adhesive_code adc2
FROM match_table
LEFT JOIN adhesive_table a ON match_table.match_1 = a.adhesive_code
LEFT JOIN adhesive_table b ON match_table.match_2 = b.adhesive_code
WHERE
match_table.brand_id IN ($b)";

2 个答案:

答案 0 :(得分:2)

$sql="SELECT id, brand_id, sheet_name, a.adhesive_name, a.adhesive_code FROM match_table 
INNER JOIN adhesive_table a
ON match_table.match_1 = a.adhesive_code
WHERE match_table.brand_id
IN ($b)
UNION
SELECT id, brand_id, sheet_name, a.adhesive_name, a.adhesive_code FROM match_table 
INNER JOIN adhesive_table a 
ON match_table.match_2 = a.adhesive_code
WHERE match_table.brand_id
IN ($b)";

编辑:我编辑了上面的查询,以便更清楚地显示两个匹配项。另一方面,如果你想要它们在同一行,这是另一个不同的:

$sql="
SELECT id, brand_id, sheet_name, a.adhesive_name, a.adhesive_code, b.adhesive_name, b.adhesive_code 
FROM match_table 
    LEFT JOIN adhesive_table a ON match_table.match_1 = a.adhesive_code
    LEFT JOIN adhesive_table b ON match_table.match_2 = b.adhesive_code
WHERE 
    match_table.brand_id IN ($b)
AND (a.id IS NOT NULL OR b.id IS NOT NULL)";

答案 1 :(得分:0)

你的第一次尝试很好,除了一个细节:如果你在同一张桌子上加入两次,请使用别名:

SELECT * FROM match_table
LEFT JOIN adhesive_table ad1 ON match_table.match_1 = ad1.adhesive_code
LEFT JOIN adhesive_table ad2 ON match_table.match_2 = ad2.adhesive_code
WHERE match_table.brand_id=1;