gpt4 book ai didi

MySQL LEFT OUTER JOIN 与 LIMIT?

转载 作者:行者123 更新时间:2023-11-30 23:28:22 24 4
gpt4 key购买 nike

这是我的问题

SELECT `t`.`id` AS `t0_c0`
, `news`.`id` AS `t2_c0`
, `news`.`title` AS `t2_c1`
, `news`.`url` AS `t2_c2`
, `news`.`date` AS `t2_c3`
, `news`.`hash` AS `t2_c4`
, `news`.`list_id` AS `t2_c5`
FROM
`words` `t`
LEFT OUTER JOIN `words_news` `news_news`
ON (`t`.`id` = `news_news`.`word_id`)
LEFT OUTER JOIN `news` `news`
ON (`news`.`id` = `news_news`.`news_id`)
WHERE
(`t`.`id` IN (8229, 8574, 7901))

而且我只需要从news 表中选择5 条新闻,但不知道如何,我应该如何编写查询?

最佳答案

试试这个:

    SELECT `t`.`id` AS `t0_c0`
, `news`.`id` AS `t2_c0`
, `news`.`title` AS `t2_c1`
, `news`.`url` AS `t2_c2`
, `news`.`date` AS `t2_c3`
, `news`.`hash` AS `t2_c4`
, `news`.`list_id` AS `t2_c5`
FROM
`words` `t`
LEFT OUTER JOIN `words_news` `news_news`
ON (`t`.`id` = `news_news`.`word_id`)
LEFT OUTER JOIN (SELECT * FROM `news` LIMIT 5) `news`
ON (`news`.`id` = `news_news`.`news_id`)
WHERE
(`t`.`id` IN (8229, 8574, 7901));

SELECT `t`.`id` AS `t0_c0`
, `news`.`id` AS `t2_c0`
, `news`.`title` AS `t2_c1`
, `news`.`url` AS `t2_c2`
, `news`.`date` AS `t2_c3`
, `news`.`hash` AS `t2_c4`
, `news`.`list_id` AS `t2_c5`
FROM
`words` `t`
LEFT OUTER JOIN `words_news` `news_news`
ON (`t`.`id` = `news_news`.`word_id`)
LEFT OUTER JOIN `news` `news`
ON (`news`.`id` = `news_news`.`news_id`)
JOIN (SELECT @var_ctr := 0) a
WHERE IF(`news`.`id` IS NOT NULL, @var_ctr := @var_ctr + 1, @var_ctr) <= 5
AND (`t`.`id` IN (8229, 8574, 7901));

关于MySQL LEFT OUTER JOIN 与 LIMIT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11985469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com