如何正确编写MySQL别名?

时间:2014-08-25 08:56:05

标签: php mysql sql

对于所有关心的人,我发现我需要重新阅读有关查询的基础知识。排序后,我意识到我的代码中存在一些错误。

我有一个我正在尝试构建的查询,并且在出现关于歧义的错误之后我会抛出错误。有人会告诉我如何正确执行MySQL别名吗?

到目前为止,这是我的代码。它工作正常,直到我把连接放在那里。我怀疑这是因为我在同一个查询中不止一次引用'wine',但这可能是许多问题之一。

$query = 
"SELECT 
    wine_id, 
    wine_name, 
    winery_name, 
    region_name, 
    year, 
    variety
FROM 
    wine AS w, 
    winery, 
    region, 
    grape_variety
JOIN 
    wine 
ON
    grape_variety.variety_id = wine.wine_id
JOIN 
    wine_variety 
ON
    wine.wine_id = wine_variety.variety_id
WHERE 
    winery.region_id = region.region_id
AND 
    wine.winery_id = winery.winery_id"; 

if (!empty($wineName)) {
    $query .= " AND wine_name = '{$wineName}'";
}

if (!empty($wineryName)) {
    $query .= " AND winery_name = '{$wineryName}'";
}

// If the user has specified a region, add the regionName 
// as an AND clause
if (isset($regionName) && $regionName != "All") {
    $query .= " AND region_name = '{$regionName}'"; 
} 

// If the user has specified a variety, add the grapeVariety 
// as an AND clause
if (isset($grapeVariety) && $grapeVariety != "Riesling") {
    $query .= " AND variety = '{$grapeVariety}'"; 
}

4 个答案:

答案 0 :(得分:1)

尝试这样做:

SELECT w.wine_id, w.wine_name,  winery_name, region_name,  year, variety
FROM 
    wine AS w 
    join winery on w.winery_id = winery.winery_id
    join region on winery.region_id = region.region_id, 
    join grape_variety on grape_variety.variety_id = w.wine_id

答案 1 :(得分:1)

您的查询必须如下:

SELECT 
    w.wine_id, 
    w.wine_name, 
    wy.winery_name,-- I assume this column from table `wy` 
    r.region_name, -- column from table `region` 
    w.year, 
    w.variety
FROM wine w 
INNER JOIN winery wy ON (wy.winery_id = w.winery_id )
INNER JOIN region r ON ( r.region_id = wy.region_id )
INNER JOIN  grape_variety gv ON (gv.variety_id = w.wine_id)
INNER JOIN  wine_variety wv ON  (wv.variety_id = w.wine_id)
...
append other WHERE conditions here

注意:如果你给表结构然后它会更清楚,首先我假设你检索的任何列是来自主表和 如果查询中的所有表都具有相同的列名,则使用table_alias_name.column_name

答案 2 :(得分:0)

试试这个

SELECT 
    w.wine_id, 
    w.wine_name, 
    wy.winery_name, 
    r.region_name, 
    r.year, 
    wv.variety
FROM wine w 
INNER JOIN winery wy ON (wy.winery_id = w.winery_id )
INNER JOIN region r ON ( r.region_id = wy.region_id )
INNER JOIN  grape_variety gv ON (gv.variety_id = w.wine_id)
INNER JOIN  wine_variety wv ON  (wv.variety_id = w.wine_id)

答案 3 :(得分:-1)

使用此

$query = 
 "SELECT 
   w.wine_id, 
   w.wine_name, 
   winery.winery_name, 
   region.region_name, 
   year, 
   grape_variety.variety
FROM 
   wine AS w, 
   winery, 
   region, 
   grape_variety
JOIN 
   wine 
ON
   grape_variety.variety_id = wine.wine_id
JOIN 
   wine_variety 
ON
   wine.wine_id = wine_variety.variety_id
WHERE 
   winery.region_id = region.region_id
AND 
   wine.winery_id = winery.winery_id";
if (!empty($wineName)) {
$query .= " AND w.wine_name = '{$wineName}'";
}

if (!empty($wineryName)) {
$query .= " AND winery.winery_name = '{$wineryName}'";
}

// If the user has specified a region, add the regionName 
// as an AND clause
if (isset($regionName) && $regionName != "All") {
$query .= " AND region.region_name = '{$regionName}'"; 
} 

// If the user has specified a variety, add the grapeVariety 
// as an AND clause
if (isset($grapeVariety) && $grapeVariety != "Riesling") {
$query .= " AND grape_variety.variety = '{$grapeVariety}'"; 
 }