gpt4 book ai didi

mysql - 从故事中选择项目,其中 story.uid 等于 uid 或 story.uid 等于 friend.uid 或 friend.fid 并被接受

转载 作者:行者123 更新时间:2023-11-29 00:35:48 29 4
gpt4 key购买 nike

如果满足条件,我如何才能只获取特定记录?

我有代码为

"SELECT a.id, a.text, a.uid, a.time 
FROM story a INNER JOIN friends b
ON ((a.uid = '" . $uid . "') OR
(b.uid = '" . $uid . "' AND accepted='1') OR
(b.fid = '" . $uid . "' AND accepted='1'))
ORDER BY a.time DESC
LIMIT 3";

除了friends 表为空时,它不会显示story 记录外,它工作正常。

我想要实现的是

  • 如果表 story 中的 uid 等于 logged in uid(用户),则显示 text 来自表 story (意味着这是他自己的故事)

  • 如果表 story 中的 uid 等于表 friends 中的 uid 及其友谊accepted OR 如果表 story 中的 uid 等于表中的 fid friends 并且它的友谊被接受 然后在表 story 中显示文本(意味着故事来自 friend 并且仅在接受友谊时显示)

  • 如果根本没有友谊,只显示故事表中的自己的故事并忽略其他

最佳答案

我认为您没有使用正确的连接格式。它应该是:

    SELECT field_name 
FROM table1 t1
INNER JOIN table2 t2
ON t1.id = t2.some_id
WHERE..

可以查基础here .

在您的情况下,您应该按如下方式编写查询:

    "SELECT a.id, a.text, a.uid, a.time 
FROM story a INNER JOIN friends b
ON a.id = b.some_id WHERE ((a.uid = '" . $uid . "') OR
(b.uid = '" . $uid . "' AND accepted='1') OR
(b.fid = '" . $uid . "' AND accepted='1'))
ORDER BY a.time DESC
LIMIT 3";

我认为这会有所帮助。

关于mysql - 从故事中选择项目,其中 story.uid 等于 uid 或 story.uid 等于 friend.uid 或 friend.fid 并被接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14353647/

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