我是新来的,就像发布问题一样,但我很久以前就一直在寻找答案。 我现在还想做什么,我找不到正确的解决方案,或者我只是不知道如果我找到了它就会使用它。
这是我的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都保存为SLINK ID,其中包含用户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'和slink_id ='$ surflink'))按临时限制1“的订单;
答案 0 :(得分: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";
看看它是否解决了你的问题
答案 1 :(得分:0)
如果您的链接ID列在表table_links
中名为my_link_id,那么请使用此类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";
答案 2 :(得分:0)
$sql = "SELECT * FROM table_link1
WHERE id IN (SELECT id FROM table_link2) ORDER BY RAND()" LIMIT 1;