gpt4 book ai didi

mysql - 从两个表中选择单个值,同时从第三个表中获取条件

转载 作者:行者123 更新时间:2023-11-29 02:31:07 24 4
gpt4 key购买 nike

我有一个令我困惑的 MySQL 查询。我试图从两个不同的表(blurbs 和 users)中获取特定的信息,同时将其限制为仅关注的人。

我有以下查询:

SELECT DISTINCT blurbs.text, blurbs.timestamp, users.name, 
users.username, users.profilepic, users.id
FROM blurbs,users
LEFT OUTER JOIN follows
ON blurbs.uid = follows.following AND follows.follower = ?
WHERE (blurbs.uid = $user_id OR follows.following IS NOT NULL)
AND (LOWER(blurbs.text) LIKE '%$query%' OR LOWER(users.name) LIKE '%$query%')
AND blurbs.is_private=0 AND blurbs.uid=users.id
LIMIT 0,30

它不能正常工作,但由于连接,我变得过于困惑。

我应该怎么做才能解决这个问题?

最佳答案

尝试仅通过 where 子句使用所有连接条件,例如

      select a.*, b.*     //<-- Selectign data from first two tables
from table1 a, table2 b, table1 c
where a.id = b.id //<-- joining condition for first two tables
and a.xxx= c.yyyyy //some relation with third table
and c.aaa =.... //filter conditions using third table
and c.bbb =... //filter conditions using third table

尝试将您的查询改写如下:

   SELECT DISTINCT blurbs.text, blurbs.timestamp, users.name, 
users.username, users.profilepic, users.id
FROM blurbs,users, follows
WHERE blurbs.uid=users.id
AND blurbs.uid = follows.following
AND follows.follower = ?
AND (blurbs.uid = $user_id OR follows.following IS NOT NULL)
AND (LOWER(blurbs.text) LIKE '%$query%' OR LOWER(users.name) LIKE '%$query%')
AND blurbs.is_private=0
LIMIT 0,30

完成后,希望您可以更好地控制/管理所需的过滤条件。

关于mysql - 从两个表中选择单个值,同时从第三个表中获取条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022851/

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