1

我目前有一个带有数组的查询,该数组在我的表单中使用动态输入输出变量(term),这会创建一个具有自动完成功能的动态搜索来填写产品的所有详细信息。

    $return_arr = array();
$param = $_GET["term"];
    $fetch = mysql_query("SELECT * FROM crd_jshopping_products WHERE `name_en-GB` REGEXP '^$param'");
    while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
    //$row_array['category_id']  = $row ['category_id'];

$row_array['product_id'] = $row['product_id'];
    $row_array['product_names']  = $row['name_en-GB'];
    $row_array['jshop_code_prod'] = $row['product_ean'];
    $row_array['_ext_price_html']  = number_format($row['product_price'],2);
if (!empty($row['product_thumb_image']) AND isset($row['product_thumb_image'])){
$row_array['image'] = $row['product_thumb_image'];      
}else {
    $row_array['image'] = 'noimage.gif';    
}
    array_push( $return_arr, $row_array);
}
mysql_close($conn);
echo json_encode($return_arr);

不幸的是,我还需要得到category_idwhich is not in the same table,我试图修改我query的,但无济于事:

$fetch = mysql_query("SELECT * FROM crd_jshopping_products WHERE `name_en-GB` REGEXP '^$param' AND `crd_jshopping_products_to_categories`  = `product_id` ");

我在这里缺少什么步骤?双方的product_id's比赛tables

4

1 回答 1

1

试试这个查询,并尝试理解我在其中写的内容:

$fetch = mysql_query("
    SELECT
        p.*,
        c.category_id
    FROM
        crd_jshopping_products as p
        INNER JOIN crd_jshopping_products_to_categories as c
            ON p.product_id = c.product_id
    WHERE
        `p.name_en-GB` REGEXP '^$param'
");

这表示:

SELECT: 给我p中的所有内容和c中的 category_id 。

FROM: 从表 crd_jshopping_products(称为 p)和 crd_jshopping_products_to_categories(称为 c)中的行执行此操作,其中行匹配的p .product_id 计数与c .product_id相同。

WHERE: 仅返回p .name_en-GB REGEXP '^$param' 所在的行。

于 2013-05-22T07:24:09.763 回答