0

对于发布问题,我是新来的,但我很久以前就一直在寻找答案。仍然我现在正在尝试做的事情是我没有找到正确的解决方案,或者我只是不知道如果我找到了如何使用它。

这是我的 SQL 查询:

$sql = "SELECT * FROM table_links AS link_id WHERE link_points > '1' 
AND NOT EXISTS (SELECT * FROM table_surf AS slink_id WHERE user_id = '$user'
AND surf_time = '$time') ORDER by RAND() LIMIT 1";

我正在尝试选择一个随机的 LINK id(有积分),并通过查看第二个表来检查该链接是否已经显示给用户,其中所有 LINK ID 都保存为带有用户 ID 和时间的 SLINK ID访问。

如果上次访问是今天($time),那么它不应该返回任何东西。

我是 PHP/mysql 的新手,但我正在努力学习,但在这种情况下,我仍然不确定是否有可能实现像这样的复杂查询。

更新:根据 Akhil 的帖子,这似乎对我有用 -

$sql = "SELECT * FROM (SELECT RAND() temp, link_id FROM table_links WHERE link_points > '1' AND link_status NOT IN ('spam') AND NOT EXISTS (SELECT slink_id FROM table_surf AS links WHERE user_id = '$user' AND surf_time = '$time' AND slink_id = '$surflink')) 临时限制 1 的订单";

4

3 回答 3

1

使用子查询

$sql = "select * from (SELECT RAND() temp, table_links.* FROM table_links AS link_id WHERE link_points > '1' 
AND NOT EXISTS (SELECT * FROM table_surf AS slink_id WHERE user_id = '$user'
AND surf_time = '$time')) a  ORDER by temp LIMIT 1";

看看能不能解决你的问题

于 2013-08-10T07:43:59.647 回答
0

如果您的链接 id 列在表中称为 my_link_id ,table_links则使用NOT IN这样的

$sql = "SELECT * FROM table_links AS link_id WHERE link_points > '1' AND link_id.my_link_id NOT IN (SELECT * FROM table_surf AS slink_id WHERE user_id = '$user' AND surf_time = '$time') ORDER by RAND() LIMIT 1";

于 2013-08-10T07:39:57.643 回答
0
$sql = "SELECT * FROM table_link1 
        WHERE id IN (SELECT id FROM table_link2) ORDER BY RAND()" LIMIT 1;
于 2013-08-10T08:08:04.190 回答