我是mysql初学者,请问这种情况能不能帮我调用query
我有2张表,我让id自动递增
表 1:closed_route
= id | name | lat
and the query is =
=1|kenjeran 1|-7.2499|-7.249
=2|mulyosari 2|-7.259|-7.259
表 2: 用户位置
= id | lat | long1 | lat2 | long2
and the query is =
=18|-7.24945|112.783|-7.25907|112.795
表 3: 标记
=id | lat | lng | name
我想问的是: 如何使用该表在我的表 3 中插入列
我用这段代码但是结果是0,其实那个表有查询
INSERT INTO markers (lat,lng,name) SELECT user_location.lat, user_location.long1, closed_route.name
FROM user_location, closed_route
where user_location.lat LIKE CONCAT ('%', closed_route.lat, '%')LIMIT 1;
最佳答案
下面是一种应该有效的查询形式:
INSERT INTO markers (lat, lng, name)
SELECT ul.lat, ul.long1, cr.name
FROM user_location ul cross join
closed_route cr
ORDER BY ABS(ul.lat - cr.lat)
LIMIT 1;
这将插入一个标记,其名称为 closed_route
,该标记具有最接近的 lat
值。
注意事项:
- 此查询使用显式
连接
。只是不要在from
子句中使用逗号。始终使用明确的join
语法。 - 这些表有别名,因此查询更容易编写和阅读。
- 没有对数值使用
like
。相反,请使用数字函数。 - 这可能无法满足您的要求,但它似乎是一个合理的查询。
关于php - 如何从另一个表调用查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229322/