【问题标题】:using first sql statement result into another sql statement使用第一个 sql 语句结果到另一个 sql 语句
【发布时间】:2017-02-17 23:59:42
【问题描述】:

基本上我想做的是从roadData中挑选所有坐标 一个一个然后在20以内找到tweetMelbourne中的所有点 英里,然后将这些点插入另一个表。

所以对于roadData 表中的每个(x,y),从 tweetMelbourne 并将这些点插入到另一个新表中。

所以我必须这样做:

SELECT geo_coordinates_latitude, geo_coordinates_longitude
 FROM tweetmelbourne
 HAVING  ( 3959 * acos( cos( radians(latitude) ) * cos( radians( geo_coordinates_latitude ) ) * 
 cos( radians( geo_coordinates_longitude ) - radians(longitude) ) + sin( radians(latitude) ) *
 sin( radians( geo_coordinates_latitude ) ) ) ) < .1 ORDER BY distance LIMIT 0 , 20; 

其中纬度和经度的值我必须从另一个表中获取:

select longitude,latitude from roadData;

描述tweetmelbourne;

描述道路数据;

SELECT geo_coordinates_latitude, geo_coordinates_longitude
FROM tweetmelbourne;

select longitude,latitude from roadData;

【问题讨论】:

  • 你需要把它分成两个子查询,WHERE x in (...) AND y in (...)
  • 这毫无意义。 WHERE 子句用于选择student 中的行。
  • studenttweet 表是如何相互关联的? tweet 表中是否有 student_id 列?
  • 但我只想这样做,所以让第一个查询的输出为 5 行。现在我想对每 5 行运行第二个查询。
  • 您不应该发布新问题,您应该编辑原始问题。但他们都不是很清楚你真正想要做什么。你好像不明白WHERE在SQL中是什么意思,这是一个很基础的东西。

标签: mysql sql phpmyadmin mysql-workbench


【解决方案1】:

您需要连接两个表,计算ON 子句中的距离以选择附近的行。

SELECT *
FROM tweetmelbourne
JOIN roadData
ON ( 3959 * acos( cos( radians(latitude) ) * cos( radians( geo_coordinates_latitude ) ) * 
    cos( radians( geo_coordinates_longitude ) - radians(longitude) ) + sin( radians(latitude) ) *
    sin( radians( geo_coordinates_latitude ) ) ) ) < .1

如果表很大,这将非常慢。不可能使用索引来实现连接,所以它必须对每一对行执行那个复杂的公式。您可能想查看 MySQL 的 Spatial Data 扩展。

【讨论】:

  • SELECT geo_coordinates_latitude, geo_coordinates_longitude, ( 3959 * acos( cos( 弧度(纬度) ) * cos( 弧度( geo_coordinates_latitude ) ) * cos( 弧度( geo_coordinates_longitude ) - 弧度(经度) ) + sin( 弧度(纬度) ) * sin( 弧度( geo_coordinates_latitude ) ) ) ) 到 roadTweets FROM tweetmelbourne cross join roadData HAVING distance
  • 我觉得这样也不错。是的,它非常慢.. 如何使用 spatia;数据。我会试试看,然后问你是否可以。
【解决方案2】:

试试这个:

SELECT s.address, (t.x + t.y) as z
from (SELECT id,x,y FROM `tweet`) as t, student s
WHERE t.id = s.id;

【讨论】:

    【解决方案3】:

    IN() 带有多个参数的正确语法是:(Val1,Val2) IN(SELECT VAL1,val2..

    SELECT t.address,(t.x+t.y) as z 
    FROM student t
    WHERE (t.x,t.y) IN(SELECT x,y FROM tweet)
    

    也可以通过连接来完成:

    SELECT t.address,(t.x+t.y) as z 
    FROM student t
    JOIN tweet s
     ON(t.x = s.x and t.y = s.y)
    

    编辑:我认为你想要的是:

    SELECT s.address,t.x+t.y as z
    FROM student s
    CROSS JOIN tweet t
    

    【讨论】:

    • 错误代码:1054。“字段列表”中的未知列“x”
    • 学生表中是否有 x 列? @ArjunChaudhary
    • 不。我想使用第二个查询的输出作为第一个查询的输入。
    • 基本上我的主要问题是这个,但我认为没有人想阅读复杂的内容,所以我缩短了它。 stackoverflow.com/questions/39943264/…
    • 这没有任何意义,where 子句基本上是:`SELECT .. WHERE IN() 。这将永远是真的。请解释一下你想做什么。
    猜你喜欢
    • 2021-08-04
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 2012-11-21
    • 2021-03-21
    • 2023-03-21
    • 2021-11-03
    • 2015-02-06
    相关资源
    最近更新 更多