Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL选择分页限制_Mysql_Select_Join_Pagination_Unique - Fatal编程技术网

MySQL选择分页限制

MySQL选择分页限制,mysql,select,join,pagination,unique,Mysql,Select,Join,Pagination,Unique,我有一个从我的表中选择的结果: id tag 1 hey 1 wow 2 cry 87 game 87 bmw 6 lady 7 wow 但是这个结果我得到了加入。 我的完整SQL查询: SELECT sites.id, sites.host, sites.url, sites.password, sites.status, sites.pr, sites.tyc, sites.alexa, sites.lastcheck, sites.comment, tags

我有一个从我的表中选择的结果:

id  tag
1   hey
1   wow
2   cry
87   game
87   bmw
6   lady
7   wow
但是这个结果我得到了加入。 我的完整SQL查询:

SELECT sites.id, sites.host, sites.url, sites.password, sites.status, sites.pr, sites.tyc, sites.alexa, sites.lastcheck, sites.comment, tags.tagname, tags.tagid FROM sites
LEFT JOIN sites_tags ON sites_tags.shellid = sites.id 
LEFT JOIN tags ON sites_tags.tagid = tags.tagid
对于分页,我需要查询中的3个实际数据

如果我的查询限制为3,我希望

1   hey
1   wow
2   cry
87   game
87   bmw
但不是

 1   hey
 1   wow
 2   cry
例如,我需要所有结果受ID限制,而不是受行限制

抱歉英语不好。

(猜测
id
指的是
sites.id

将站点中的
替换为:

FROM ( SELECT * 
       FROM sites 
       ORDER BY whatever 
       LIMIT 3
     ) AS sites
这假设您想要的是与3个站点精确相关的标签。这将选择3个站点的站点信息,然后还将选择任何标签(如果有)

但是,它不会准确返回您想要的内容,您最好执行一次查询,以选择
站点中的所有字段

SELECT
    id, 
    host, 
    url, 
    password, 
    status, 
    pr, 
    tyc, 
    alexa, 
    lastcheck, 
    comment
FROM sites
LIMIT 3
然后第二次执行以下操作以获取这些站点的标记

SELECT 
    sites_tags.shellid AS id, 
    tags.tagname, 
    tags.tagid 
FROM sites_tags
INNER JOIN tags ON sites_tags.tagid = tags.tagid
WHERE sites_tags.shellid IN (..... your ids go here ......)

您给出的查询不会生成您显示的示例数据集。不知道这是否是最好的解决方案,但我习惯于为具有多个联接的查询创建视图。限制没有问题,而且当我需要直接在数据库中检查某些内容时,我可以在特殊的位置轻松地使用它
SELECT 
    sites_tags.shellid AS id, 
    tags.tagname, 
    tags.tagid 
FROM sites_tags
INNER JOIN tags ON sites_tags.tagid = tags.tagid
WHERE sites_tags.shellid IN (..... your ids go here ......)