我有一个显示国家鸟类列表的数据库查询。我选择了一个添加了很酷细节的脚本:能够为代表不止一个国家的鸟类添加星号,星星等。
但是,我必须在我的代码或数据库表中更改了一些内容。我现在收到错误消息未定义索引:星号。有谁能发现问题?
我将查询粘贴到phpMyAdmin> SQL,它似乎工作正常,显示代表国家鸟类的行......
SELECT GS.N, GS.IDArea SymArea, GS.IDSymbol, GS.URL, GS.Title,
GS.PageKind3, GS.Symbol, GS.Latin, GS.Desig, GS.DesigGen, GS.DesigGroup, GS.Date,
GG.N, GG.IDArea, GG.Name, GG.Type, GG.IDParent, GG.IDParent2, GG.Parent, GG.Parent2, GG.IDReg,
T.Stars
FROM gs AS GS
LEFT JOIN gw_geog AS GG ON GG.IDArea = GS.IDArea
LEFT JOIN (
SELECT Latin, COUNT(Latin) as Stars
FROM gs
GROUP BY Latin
) as T ON GS.Latin = T.Latin
WHERE GS.DesigGen = 'bird' AND GG.Type = 'nat' AND GS.Symbol != ''
OR GS.DesigGen = 'bird' AND GG.Type = 'dep' AND GS.Symbol != ''
GROUP BY GS.IDArea ORDER BY GS.N
最重要的价值可能是GS.Latin,它简单地列出了各种鸟类。拉丁语或科学名称。看来上面的查询并没有“连接”#34;我的循环中的脚本,下面。有人有预感问题是什么吗?
while ($row = $stm->fetch())
{
switch (TRUE)
{
case ($row['Stars'] == 2):
$star_rating = ' (2)';
break;
case ($row['Stars'] == 3):
$star_rating = ' (3)';
break;
case ($row['Stars'] == 4):
$star_rating = ' (4)';
break;
case ($row['Stars'] > 5):
$star_rating = ' (5 or more)';
break;
default:
$star_rating = '';
break;
}
}
答案 0 :(得分:1)
我认为您的左连接会将Stars
字段设为null,尝试将查询字段中的T.Stars
替换为
COALESCE(T.Stars,0) AS `Stars`
如果字段为空,这将设置默认值0
答案 1 :(得分:1)
您忘了将T.Latin
放在查询的选定部分,例如
SELECT GS.N, GS.IDArea SymArea, GS.IDSymbol, GS.URL, GS.Title,
GS.PageKind3, GS.Symbol, GS.Latin, GS.Desig, GS.DesigGen, GS.DesigGroup, GS.Date,
GG.N, GG.IDArea, GG.Name, GG.Type, GG.IDParent, GG.IDParent2, GG.Parent, GG.Parent2, GG.IDReg,
T.Stars, T.Latin
FROM gs AS GS
LEFT JOIN gw_geog AS GG ON GG.IDArea = GS.IDArea
LEFT JOIN (
SELECT Latin, COUNT(Latin) as Stars
FROM gs
GROUP BY Latin
) as T ON GS.Latin = T.Latin
WHERE GS.DesigGen = 'bird' AND GG.Type = 'nat' AND GS.Symbol != ''
OR GS.DesigGen = 'bird' AND GG.Type = 'dep' AND GS.Symbol != ''
GROUP BY GS.IDArea ORDER BY GS.N