表达式引擎 SQL 查询条目作者列表

发布于 2024-11-29 09:08:12 字数 333 浏览 1 评论 0原文

我如何从一个频道订购最新的 3 个条目,每个条目都来自不同的作者? (所以它们最终不会成为同一作者的 3 个最新条目) - 我想我需要使用 SQL 查询吗?

{exp:channel:entries orderby="screen_name|date" channel="portfolios" limit="3" group_id="5" dynamic="no"}
<img src=" {thumbnail}" alt="{title}"/><br />
{title}<br />
{/exp:channel:entries} 

提前致谢!

How would I order the latest 3 entries from a channel with each entry from a different author? (so they don't end up being 3 latest entries by the same author) - I think I need to use an SQL Query for it?

{exp:channel:entries orderby="screen_name|date" channel="portfolios" limit="3" group_id="5" dynamic="no"}
<img src=" {thumbnail}" alt="{title}"/><br />
{title}<br />
{/exp:channel:entries} 

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

喵星人汪星人 2024-12-06 09:08:12

马克 - 这是我在 其他骗局上发布的答案的转发问题


这里最好的方法是,因为您需要解析自定义字段,所以首先找到来自不同作者的最新 4 个条目的entry_ids,然后将它们传递给 channel:entries标记通过使用 entry_id 参数嵌入。

这应该可以工作(请务必将 channel_id 替换为适当的整数)。将当前的整个代码块替换为:

{embed="embeds/_latest_per_member" entry_ids="{exp:query sql="SELECT entry_id, author_id FROM exp_channel_titles WHERE entry_date IN( SELECT MAX(entry_date) FROM exp_channel_titles  WHERE status != 'closed' AND channel_id = 1 GROUP BY author_id ) ORDER BY entry_date DESC LIMIT 4" backspace="1"}{entry_id}|{/exp:query}"}

然后您的 embeds/_latest_per_member 模板可能如下所示:

{exp:channel:entries channel="channel_name" entry_id="{embed:entry_ids}"}
    {author_id}<br />
    <a href="{path=portfolios/gallery/{username}}"><img src="{thumbnail}"></a><br>
    <a href="{path=portfolios/gallery/{username}}">{title}</a><br />
{/exp:channel:entries}

您曾提到此代码给您带来了递归错误 - 这意味着您已经进行了另一个调用到嵌入的嵌入。不要那样做。

Mark - here's a repost of the answer I posted on the other dupe question:


The best approach here, since you need your custom fields parsed, is to first find the entry_ids of the latest 4 entries from distinct authors, and then pass those to the channel:entries tag through an embed using the entry_id parameter.

This should work (be sure to replace the channel_id with the appropriate integer). Replace your entire current chunk of code with this:

{embed="embeds/_latest_per_member" entry_ids="{exp:query sql="SELECT entry_id, author_id FROM exp_channel_titles WHERE entry_date IN( SELECT MAX(entry_date) FROM exp_channel_titles  WHERE status != 'closed' AND channel_id = 1 GROUP BY author_id ) ORDER BY entry_date DESC LIMIT 4" backspace="1"}{entry_id}|{/exp:query}"}

Then your embeds/_latest_per_member template can look something like this:

{exp:channel:entries channel="channel_name" entry_id="{embed:entry_ids}"}
    {author_id}<br />
    <a href="{path=portfolios/gallery/{username}}"><img src="{thumbnail}"></a><br>
    <a href="{path=portfolios/gallery/{username}}">{title}</a><br />
{/exp:channel:entries}

You had mentioned that this code gave you a recursive error - that means that you've put another call to the embed within the embed. Don't do that.

鸩远一方 2024-12-06 09:08:12

WHERE IN (SELECT MAX(entry_date)...) 示例对数据库造成了巨大的影响。它似乎本质上为每个条目执行一个子查询。我发现的另一种选择(Stackoverflow)仅在 FROM 部分中使用一个子查询,

引用:Do a GROUP BY after通过用 GROUP BY 包装查询来实现 ORDER BY:-)。

无论如何......这会获取所有作者及其最后发布的条目标题。如果您还需要 url_title,则必须将其添加到两个 SELECT 语句中。我添加了一些额外的选项,仅获取最近 4 个月的数据,仅限于channel_id 8,并仅限于category_id 68。

SELECT author_id, screen_name, title, FROM_UNIXTIME(entry_date) AS m_date 
FROM (
  SELECT t.author_id, t.title, m.screen_name, t.entry_date
  FROM exp_channel_titles AS t
  LEFT JOIN exp_members AS m ON t.author_id = m.member_id
  LEFT JOIN exp_category_posts AS c ON c.entry_id = t.entry_id
  WHERE t.entry_date > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 4 MONTH)) 
  AND t.channel_id = 8
  AND c.cat_id = 68
  ORDER BY t.entry_date DESC
) AS S 
GROUP BY S.author_id
ORDER BY entry_date DESC
# LIMIT 10

The WHERE IN (SELECT MAX(entry_date)...) example took a huge hit at the database. It seems to essentially do a subquery for each entry. An alternative i found (Stackoverflow) uses just one subquery in the FROM part,

quote: Do a GROUP BY after the ORDER BY by wrapping your query with the GROUP BY :-).

Anyway ... This fetches all authors and their last posted entry title. If you also want the url_title, you have to add it to both SELECT statements. I've included some extra options, fetch only for the last 4 months, limited to channel_id 8, and limit to category_id 68.

SELECT author_id, screen_name, title, FROM_UNIXTIME(entry_date) AS m_date 
FROM (
  SELECT t.author_id, t.title, m.screen_name, t.entry_date
  FROM exp_channel_titles AS t
  LEFT JOIN exp_members AS m ON t.author_id = m.member_id
  LEFT JOIN exp_category_posts AS c ON c.entry_id = t.entry_id
  WHERE t.entry_date > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 4 MONTH)) 
  AND t.channel_id = 8
  AND c.cat_id = 68
  ORDER BY t.entry_date DESC
) AS S 
GROUP BY S.author_id
ORDER BY entry_date DESC
# LIMIT 10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文