gpt4 book ai didi

php - MYSQL 查询 - 列出给定距离内未被阻止的事件

转载 作者:行者123 更新时间:2023-11-29 00:14:32 25 4
gpt4 key购买 nike

我有一个非常复杂的查询,它显示所有事件,除了那些它或它的类别/类型/子类型包含在名为“已阻止”的表中的事件。此外,它仅显示事件的事件 (active=1),并且结束时间介于当前时间和当天结束之间。

SELECT e.* FROM `events` e WHERE NOT EXISTS (
SELECT 1 FROM `blocked` b
WHERE b.uid = '$uid'
AND (e.bid = b.bid OR e.category = b.category
OR e.type = b.type OR e.subtype = b.subtype))
AND active = '1'
AND end >= '$timeinfo[0]'
AND end <= '$timeinfo[2]'

现在,我还有另一个查询,它只显示给定距离内的所有事件。这些按距离排序。

SELECT *, ( 3959 * acos( cos( radians($items[3]) ) * 
cos( radians( latitude ) ) * cos( radians( longitude )
- radians($items[4]) ) + sin( radians($items[3]) ) *
sin( radians( latitude ) ) ) )
AS distance FROM events HAVING distance < $distance
ORDER BY distance LIMIT 0 , $numberofhits;

但是,现在我需要将这两者结合起来。我和他们扯了一点,但还没有得到任何接近工作的东西。从头开始研究它们会更好吗?

下面是两个表的相关部分:

EVENTS: 
eid | bid | category | type | subtype | latitude | longitude | start | end
001 | 00a | Nightlife | Bar | Karaoke | 33.457573 | -122.243475 | 1398686400 | 1398786400

BLOCKED:
uid | bid | category | type | subtype
001 | null | null | null | Karaoke
001 | 00a | null | null | null
001 | 00h | null | null | null
002 | null | null | Bar | null

在被阻止的表中,用户 001 不想看到来自 BID 00a 和 00h 的任何内容,也不想看到子类型“卡拉 OK”(糟糕的歌声)的任何内容。然后,第二个用户 002 不想看到任何类型为“Bar”的东西。

我最终要显示的是 $mycoords[0]、$mycoords[1](纬度/经度)$distance 英里范围内的所有事件,除了 BID/类别/类型/子类型所在的位置为 $uid 阻塞,按距离排序。如果你们能给我任何帮助,我将不胜感激。

最佳答案

您应该能够简单地将距离计算连同相关条件添加到“已阻止”检查查询中。

我试图在 sql fiddle 上复制您的示例数据 http://sqlfiddle.com/#!2/90bab/8并将它们结合起来。您可以将用户 ID 修改为 001 或 002 以查看不同的结果。

为了引用,结果查询是

SELECT e.*,
( 3959 * acos( cos( radians(-122.243475) ) *
cos( radians( latitude ) ) * cos( radians( longitude )
- radians(33.457573) ) + sin(-122.243475) ) *
sin( radians( latitude ) ) )
AS distance
FROM `events` e
WHERE
NOT EXISTS (
SELECT 1 FROM `blocked` b
WHERE b.uid = '001'
AND (e.bid = b.bid OR e.category = b.category
OR e.type = b.type OR e.subtype = b.subtype)
)
AND active = '1'
AND end >= 1398786400
AND end <= 1398786400

HAVING distance < 10000000000000;

请注意为了示例,我必须删除您的 php 变量并执行以下操作

  • 添加了一些经/纬度坐标示例
  • 添加了示例距离
  • 删除了限制
  • 添加了一些示例时间
  • 添加了示例用户 ID。

关于php - MYSQL 查询 - 列出给定距离内未被阻止的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23462881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com