mysql。随机订购位置并加入列表
我有两个表:位置和列表。
地点id
标题
地址
纬度
经度
列表id
location
info
status
SELECT locations.title,
locations.address,
( 3959 * acos( cos( radians('".$center_lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$center_lng."') ) + sin( radians('".$center_lat."') ) * sin( radians( latitude ) ) ) ) AS distance
FROM locations
ORDER BY distance
这将按位置顺序列出位置,并提供用户提供的纬度和经度。工作完美,但我真正想做的是..
- 为每个位置列出一个“列表”,并让位置保持顺序。
- 如果一个位置有多个“列表”,则它是完全随机的。
在一个 SQL 查询中完成这一切会更好吗? 或者填充至少有一个“列表”的所有位置,然后使用另一个查询为该位置选择随机“列表”?
更新
提供创建表:
CREATE TABLE `listings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`token` varchar(4) DEFAULT NULL,
`location` varchar(45) DEFAULT NULL,
`info` varchar(45) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
CREATE TABLE `locations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(45) DEFAULT NULL,
`address_street` varchar(45) DEFAULT NULL,
`addrees_city` varchar(45) DEFAULT NULL,
`address_state` varchar(45) DEFAULT NULL,
`address_zip` varchar(45) DEFAULT NULL,
`latitude` decimal(10,6) DEFAULT NULL,
`longitude` decimal(10,6) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
I have two tables: locations and listings.
locationsid
title
address
latitude
longitude
listingsid
location
info
status
SELECT locations.title,
locations.address,
( 3959 * acos( cos( radians('".$center_lat."') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('".$center_lng."') ) + sin( radians('".$center_lat."') ) * sin( radians( latitude ) ) ) ) AS distance
FROM locations
ORDER BY distance
This will list the locations in order by location with the users provided latitude and longitude. Works perfect, but what I really want to do is..
- List one "listings" per location, and have the locations remain in order.
- If a location has more then one "listings" have it be completely random.
Would it be better to do this all in one SQL query?
Or populate all the locations that have atleast one "listings", then use another query to select a random "listings" for that location?
UPDATE
Provided create table:
CREATE TABLE `listings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`token` varchar(4) DEFAULT NULL,
`location` varchar(45) DEFAULT NULL,
`info` varchar(45) DEFAULT NULL,
`status` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=utf8;
CREATE TABLE `locations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(45) DEFAULT NULL,
`address_street` varchar(45) DEFAULT NULL,
`addrees_city` varchar(45) DEFAULT NULL,
`address_state` varchar(45) DEFAULT NULL,
`address_zip` varchar(45) DEFAULT NULL,
`latitude` decimal(10,6) DEFAULT NULL,
`longitude` decimal(10,6) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果该位置在“listings”表中至少有 1 行与其关联,则要输出locations.title,请使用:
使用:
使用 MySQL 5.1.49-community,我已通过上面的查询成功呈现了所需的结果。
我无法使用以下命令重现OP的重复行:
CREATE TABLES
INSERT语句:
To output the locations.title if the location has at least 1 row associated with it in the "listings" table, use:
Use:
Using MySQL 5.1.49-community, I've successfully rendered the desired results with the query above.
I'm unable to reproduce the OP's duplicated row using:
CREATE TABLES
INSERT statements: