简体   繁体   English

从每个类别中选择最后3个新闻 - 两个表 - (MySQL - PHP)

[英]Select The last 3 news from each category - Two tables - (MySQL - PHP)

I want to select the last 3 news from each category, from two table 我想从两个表中选择每个类别的最后3个新闻

first table ' Categories ' & ' news '

categories table 类别表

ID_CAT | NAME | PRIORITE

News table 新闻表

ID | ID_CAT | TITLE | THE_NEWS

I try to do limit in this SQL code, but it gives me just the last 3 news from all categories 我尝试在这个SQL代码中做限制,但它只给了我所有类别的最后3个新闻

SELECT C.PRIORITE, N.* 
  FROM categories C, news N 
 WHERE N.ID_CAT=C.ID_CAT 
   AND C.PRIORITE >1 
ORDER BY N.ID DESC 
LIMIT 3

I try to get all news that have PRIORITE > 1 ( The priorite is the order of category ) So, I want to get 3 last news from each priorite. 我试图获得所有PRIORITE > 1新闻(优先级是类别的顺序)所以,我想从每个优先级获得3个最后的消息。

Example : 示例:

Priorite 2 = get last 3 news
Priorite 3 = get last 3 news 

... etc ......等

I made some search on the internet, but nothing worked for me, is there any solution? 我在互联网上搜索了一些,但没有什么对我有用,有什么解决方案吗? Or do I need to create another function to get news from each category with ID sending in parameters of it? 或者我是否需要创建另一个函数来从每个类别中获取带有ID参数的新闻?

You could get n no. 你可以得到n不。 of news for each category by using a correlated sub query 通过使用相关子查询来获得每个类别的新闻

SELECT *
FROM news n
JOIN categories c ON c.id = n.category_id
WHERE c.priority > 1
AND (
  SELECT COUNT(*)
  FROM news 
  WHERE n.category_id = category_id
  AND n.id <= id
) <= 3; 

Demo 演示

Or if you are using Mysql 8 then you could make use of window functions with common table expressions like 或者,如果您使用的是Mysql 8,那么您可以使用具有常见表格表达式窗口函数

WITH cte AS(
    SELECT *, 
    RANK() OVER (PARTITION BY category_id ORDER BY id DESC ) rnk
    FROM news
    ORDER BY id

)

SELECT *
FROM cte n
JOIN categories c ON c.id = n.category_id
WHERE c.priority > 1
AND rnk <= 3;

Demo 演示

i suggest divide it 2 parts, instead of complex query, 我建议将它分为2部分,而不是复杂的查询,

in 1st part you can get categories 在第1部分,您可以获得类别

select * from Categories

then loop categories and in loop get their 3 news, 然后循环类别和循环获得他们的3个新闻,

select * from news where cat_id = '' order by desc id limit 3

if you try both and check the execution time, you see loop take less time. 如果您同时尝试并检查执行时间,您会看到循环花费的时间更少。 because news are rapidly growing stuff. 因为新闻正在快速增长。 i know you think why i call query again and again but my teacher said prefer multiple queries instead of complex one 我知道你认为为什么我一次又一次地打电话询问,但我的老师说更喜欢多个查询而不是复杂的查询

Try this query, it should work: 试试这个查询,它应该工作:

SELECT
    C.PRIORITE, N.*
FROM
    categories C
JOIN news N ON N.ID_CAT=C.ID_CAT
AND C.PRIORITE >1 
GROUP BY
    C.ID_CAT
HAVING
    COUNT(*) <= 3
ORDER BY N.ID DESC

Lemme know if there's any problem with that. Lemme知道是否有任何问题。

Try this query. 试试这个查询。 This query generate a row number for each record based on the PRIORITE. 此查询基于PRIORITE为每条记录生成行号。 Then return row number equal or less than 3. 然后返回等于或小于3的行号。

SELECT ID, PRIORITE, THE_NEWS
FROM
(
   SELECT ID, PRIORITE, THE_NEWS  ,
   @rn := IF(@prior = PRIORITE, @rn + 1, 1) AS rn,
   @prior := PRIORITE 
   FROM(
   SELECT t.*,c.PRIORITE FROM news t 
   LEFT JOIN categories c ON t.ID_CAT=c.ID_CAT ) e
   CROSS JOIN (select @rn:=0, @prior:=null) c
   ORDER BY  PRIORITE,ID desc
) as x 
WHERE x.rn <= 3 ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM