gpt4 book ai didi

MySQL JOIN 表与 WHERE 子句

转载 作者:行者123 更新时间:2023-11-29 14:38:41 25 4
gpt4 key购买 nike

我需要从两个具有不同列的 mysql 表中收集帖子,并为每组表提供一个 WHERE 子句。我感谢您的帮助,提前致谢。

这是我尝试过的...

SELECT 
blabbing.id,
blabbing.mem_id,
blabbing.the_blab,
blabbing.blab_date,
blabbing.blab_type,
blabbing.device,
blabbing.fromid,
team_blabbing.team_id
FROM
blabbing
LEFT OUTER JOIN
team_blabbing
ON team_blabbing.id = blabbing.id
WHERE
team_id IN ($team_array) ||
mem_id='$id' ||
fromid='$logOptions_id'
ORDER BY
blab_date DESC
LIMIT 20

我知道这很困惑,但我承认,我不是 mysql 老手。我充其量只是一个初学者...有什么建议吗?

最佳答案

您可以将 where 子句放在子查询中:

select
*
from
(select * from ... where ...) as alias1 -- this is a subquery
left outer join
(select * from ... where ...) as alias2 -- this is also a subquery
on
....
order by
....

请注意,您不能在 View 定义中使用这样的子查询。

您还可以组合 where 子句,如您的示例所示。使用表别名来区分不同表的列(即使不需要,使用别名也是一个好主意,因为它使内容更易于阅读)。示例:

select
*
from
<table> as alias1
left outer join
<othertable> as alias2
on
....
where
alias1.id = ... and alias2.id = ... -- aliases distinguish between ids!!
order by
....

关于MySQL JOIN 表与 WHERE 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8449355/

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