Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.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中使用LIMIT根据列值限制结果(PHP/MySQL)_Php_Mysql_Arrays_Limit - Fatal编程技术网

在MySQL中使用LIMIT根据列值限制结果(PHP/MySQL)

在MySQL中使用LIMIT根据列值限制结果(PHP/MySQL),php,mysql,arrays,limit,Php,Mysql,Arrays,Limit,我在某处和其他地方多次搜索了这个问题的答案,但没有找到一个真正符合我需要的答案。如果有,我提前道歉 我有一个使用PHP的查询,它从WordPress数据库返回一个数组。基本上我想做的是查看列的值,然后基于该值进行限制。以下是为更好地理解返回的数组: 在查询中,您会注意到,post_parent的值对几个返回的数组重复。我想做的是根据post_父项值将其限制为3,例如,我希望post_父项79、87、100等有3个条目 我不太熟悉MySQL查询,但这就是我必须得到的数组: SELECT DISTI

我在某处和其他地方多次搜索了这个问题的答案,但没有找到一个真正符合我需要的答案。如果有,我提前道歉

我有一个使用PHP的查询,它从WordPress数据库返回一个数组。基本上我想做的是查看列的值,然后基于该值进行限制。以下是为更好地理解返回的数组:

在查询中,您会注意到,post_parent的值对几个返回的数组重复。我想做的是根据post_父项值将其限制为3,例如,我希望post_父项79、87、100等有3个条目

我不太熟悉MySQL查询,但这就是我必须得到的数组:

SELECT DISTINCT ID, guid, post_parent, post_title 
FROM $wpdb->posts p 
WHERE p.post_type = 'attachment'
    AND p.post_mime_type LIKE 'image/%'
    AND p.post_status = 'inherit'
    AND p.post_parent IN
        (SELECT object_id FROM $term_relationships WHERE term_taxonomy_id = $post_term)
我试过使用分组方式,但这并没有得到我想要的。感谢您的帮助

编辑只是为了澄清,以下是我想要的结果:

这可能会起到以下作用: 我假设ID是唯一的,如果不能替代它的话

SELECT
  p.ID, guid, post_parent, post_title
FROM (
SELECT
  a.ID as ID,
  COUNT(*) as rank
FROM (
  SELECT ID, post_parent
  FROM $wpdb->posts
  WHERE post_type = 'attachment'
    AND post_mime_type LIKE 'image/%'
    AND post_status = 'inherit'
  ) AS a
JOIN (
  SELECT ID, post_parent
  FROM $wpdb->posts
  WHERE post_type = 'attachment'
    AND post_mime_type LIKE 'image/%'
    AND post_status = 'inherit'
  ) AS b ON b.ID <= a.ID AND b.post_parent = a.post_parent
GROUP BY a.ID
) AS r
JOIN $wpdb->posts p ON r.ID = p.ID AND r.rank <= 3
WHERE p.post_parent IN (
  SELECT object_id FROM $term_relationships
  WHERE term_taxonomy_id = $post_term)
GROUP BY p.ID
;
编辑:尝试在等级中包含类别,以便它实际工作


两次指定条件有点难看,但我没有看到一个简单的解决方法

你能解释一下规则吗?我的意思是,我可以给你一个查询,将其限制为三个,但这听起来不像你想要的。@Justin Wood他做到了-我想为post_父母79、87、100等创建三个条目。我建议创建一个排名,然后基于该排名使用条件。。。更改限制至少需要动态SQL。@Sam和我说过的一样,我可以创建一个可以将其限制为三个的查询,但他希望基于该字段进行查询,这意味着它可能不总是限制为三个。这有一个标记:grest-n-per-group。尝试搜索标签。是的,ID对于数据库中的每个图像都是唯一的。post_parent对于图像出现的帖子是唯一的,但是考虑到手头的问题,显然有些图像将共享相同的post_parent值。我试着按原样运行这个程序,但没有得到任何结果,不过,我会看看如何修改它,因为我看到了您正在做的事情。ebyrob您的编辑完成了我想要做的事情。非常感谢。