获取数据库表名作为列名

时间:2014-10-09 04:36:32

标签: php mysql sql database

我试图从四个表中获取数据。我成功地得到了结果。但是,我需要获取过滤结果的表名。例如,我得到了10个结果。来自TBL_CAR的3个结果,来自TBL_BIKE的3个结果,来自TBL_TRUCK的2个结果,来自TBL_BUS的2个结果。我想将不同的url链接应用于这些结果。

如何将数据库表名称作为列名称以将不同的URL链接应用于这些结果?

$sql = "(SELECT model_name, maker_url, model_url FROM ".TBL_CAR_ADD_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
    UNION
    (SELECT model_name, maker_url, model_url FROM ".TBL_BIKE_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
    UNION
    (SELECT model_name, maker_url, model_url FROM ".TBL_TRUCK_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
    UNION
    (SELECT model_name, maker_url, model_url FROM ".TBL_BUS_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)";
    $res = mysql_query($sql, $CN);
    $rows = array();
    while($row = mysql_fetch_array($res))
    {   
        if(resutls FROM TBL_CAR)
        {
        $rows[] = array('url' => asort_get_url(CAR_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_model_image_path("thumb", $row['maker_url'],$row['model_url']));
        }
        elseif(results FROM TBL_BIKE)
        {
            $rows[] = array('url' => asort_get_url(BIKE_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bike_model_image_path("thumb", $row['maker_url'],$row['model_url']));
        }
        elseif(results FROM TBL_TRUCK)
        {
            $rows[] = array('url' => asort_get_url(TRUCK_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_truck_model_image_path("thumb", $row['maker_url'],$row['model_url']));
        }
        elseif(resutls FROM TBL_BUS)
        {
            $rows[] = array('url' => asort_get_url(BUS_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bus_model_image_path("thumb", $row['maker_url'],$row['model_url']));
        }
    }
    echo stripslashes(json_encode($rows,JSON_PRETTY_PRINT));

3 个答案:

答案 0 :(得分:3)

一种简单的方法是将表名添加为查询的常量,例如:

SELECT 'TBL_CAR_ADD_MODELS' table_name, model_name, maker_url, model_url FROM ".TBL_CAR_ADD_MODELS." WHERE model_status = '1' AND       model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
UNION
(SELECT 'TBL_BIKE_MODELS',model_name, maker_url, model_url FROM ".TBL_BIKE_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
UNION
(SELECT 'TBL_TRUCK_MODELS', model_name, maker_url, model_url FROM ".TBL_TRUCK_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)
UNION
(SELECT 'TBL_BUS_MODELS', model_name, maker_url, model_url FROM ".TBL_BUS_MODELS." WHERE model_status = '1' AND model_url != '".$model_url."' AND model_name LIKE '%$q%' LIMIT 3)

通过这种方式,您只需检查table_name列以确定它来自哪个表。

答案 1 :(得分:1)

另一种方法可能是研究在数据库中创建VIEW,可能是这样的:

CREATE VIEW ITEM_MODELS AS
    SELECT *, 'cars' as tbl_name TBL_CARS
    UNION
    SELECT *, 'bikes' as tbl_name TBL_BIKE
    UNION
    SELECT *, 'trucks' as tbl_name TBL_TRUCK
    UNION
    SELECT *, 'buses'  as TBL_BUS;

这大大简化了PHP代码:

$sql = "SELECT model_name, maker_url, model_url, tbl_name FROM ITEM_MODELS  WHERE model_status = '1' AND model_url != '${model_url}' AND model_name LIKE '%${q}%' LIMIT 3)";
    while($row = mysql_fetch_array($res))
    {   
        switch($row['tbl_name']) {
            case TBL_CAR_ADD_MODELS:
                $rows[] = array('url' => asort_get_url(CAR_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_model_image_path("thumb", $row['maker_url'],$row['model_url']));
                break;

            case TBL_BIKE_MODELS:
                $rows[] = array('url' => asort_get_url(BIKE_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bike_model_image_path("thumb", $row['maker_url'],$row['model_url']));
                break;

            case TBL_TRUCK_MODELS:
                $rows[] = array('url' => asort_get_url(TRUCK_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_truck_model_image_path("thumb", $row['maker_url'],$row['model_url']));
                break;

            case TBL_BUS_MODELS:
                $rows[] = array('url' => asort_get_url(BUS_HOMEPAGE, $row['maker_url'], $row['model_url']), 'label' => $row['model_name'], 'image' => fa_bus_model_image_path("thumb", $row['maker_url'],$row['model_url']));
                break;

            default:
                // nothing sensible to do here
                break;
        }
    }

然后我们可以通过为数组查找切换这些case语句来继续简化代码,并为*_image_path函数创建一个包装器,它根据相同的{{1}调用正确的fa_FOO_model_image_path }值,也许最终让我们得到类似的东西:

$row['tbl_name']

答案 2 :(得分:0)

只需使用列名作为静态列值;

( SELECT model_name, maker_url, model_url, ".TBL_CAR_ADD_MODELS." AS table_name 
  FROM ".TBL_CAR_ADD_MODELS." WHERE model_status = '1' .....
...
if ( $rows[3] eq TBL_CAR_ADD_MODELS ) { ... }
...