如何将一些数据添加到mysql结果集中?

时间:2015-04-25 17:25:34

标签: php mysql google-maps mariadb

正如第一条评论建议的那样,我会尝试简化我的问题:

我得到了这张桌子:

  `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `name` VARCHAR( 60 ) NOT NULL ,
  `address` VARCHAR( 80 ) NOT NULL ,
  `coords` SPATIAL NOT NULL ,
  `qual` INT(3) NOT NULL;

我收到了这些数据:

Pan Africa Market, Seattle, 47.608941 -122.340145,-65
Buddha Thai & Bar, Seattle, 47.613591 -122.344394,-30
The Melting Pot, Seattle, 47.624562 -122.356442,-75
Ipanema Grill, Seattle, 47.606366 -122.337656,-90
Sake House, Seattle, 47.612820 -122.34567,-69
Crab Pot, Seattle, 47.605961 -122.34036,-70
Mama Mexican Kitchen, Seattle, 47.613975 -122.345467,-44
Wingdome, Seattle, 47.617215 -122.326584,-57
Piroshky Piroshky, Seattle, 47.610127 -122.342838,-100

如何为MariaDB或MySQL进行SQL查询,选择多边形内的所有字段和每个数据,并在结果集中创建一个新列,如下所示:

<name="Pan Africa Market" address="Seattle" coords="47.608941 -122.340141" qual="-65" icon="3"/>

有这样的条件:

if qual>-10 then new column = 0,
if -10>qual>-30 then icon = 1,
if -30>qual>-50 then icon = 2,
if -50>qual>-70 then icon = 3,
if -70>qual>-90 then icon = 4,
if -90>qual then new icon = 5

1 个答案:

答案 0 :(得分:0)

您可以使用案例陈述来执行此操作。我相信以下语句应该为您提供一个类似于您正在查找的列(您可以在select和from之间追加您在结果集中指定的其他列之间的所有内容)。

SELECT (CASE WHEN (`qual` > -10) THEN 0
WHEN (`qual` < -10 && `qual` > -30) THEN 1
WHEN (`qual` < -30 && `qual` > -50) THEN 2
WHEN (`qual` < -50 && `qual` > -70) THEN 3
WHEN (`qual` < -70 && `qual` > -90) THEN 4
WHEN (`qual` < -90) THEN 5 END) AS icon
FROM tablename

如果由于我将大部分时间花在T-SQL上,语法不太正确,我会道歉,但这应该有效。如果您需要稍微调整一下,这是CASE的资源:MySQL: Case