gpt4 book ai didi

mysql - SQL AND 无要求

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

我的网站上显示了一个表格,其中包含项目列表。下面的 SQL 语句提取每个项目并将 ###_id 列转换为另一个表中的 ###_name。到目前为止一切顺利。

我遇到的问题是,这需要填写项目表中一行中的所有字段。例如,如果项目行没有“proj_industry_id”值,则项目根本不会显示在此处。

我尝试删除 WHERE 语句中每个匹配项的“AND”并用逗号分隔它们,但它出错了。

我还检查了 SQL 文档,但似乎无法找到答案。

关于如何让我的语句在有 ID 时仍将 ID 与名称相匹配,但在没有时仍显示记录,有什么想法吗?

谢谢!

$sql = "SELECT
projects.*,
engagement_types.eng_type_name AS eng_type,
users.user_full_name AS username,
industries.industry_name AS industry_name,
categories.category_name AS category_name,
geographies.geo_name AS geo_name,
status.status_name AS status_name
FROM
projects,
engagement_types,
users,
industries,
categories,
geographies,
status
WHERE
projects.proj_eng_type_id = engagement_types.id
AND projects.proj_lead_id = users.id
AND projects.proj_industry_id = industries.id
AND projects.proj_category_id = categories.id
AND projects.proj_geo_id = geographies.id
AND projects.proj_status_id = status.id
AND projects.proj_geo_id = '$selected_geo_id'";

*****编辑******

下面是使用多个左连接的解决方案的最终正确代码!

SELECT
projects.*,
engagement_types.eng_type_name AS eng_type,
users.user_full_name AS username,
industries.industry_name AS industry_name,
categories.category_name AS category_name,
geographies.geo_name AS geo_name,
status.status_name AS status_name
FROM
projects
LEFT JOIN engagement_types ON projects.proj_eng_type_id = engagement_types.id
LEFT JOIN users ON projects.proj_lead_id = users.id
LEFT JOIN industries ON projects.proj_industry_id = industries.id
LEFT JOIN categories ON projects.proj_category_id = categories.id
LEFT JOIN geographies ON projects.proj_geo_id = geographies.id
LEFT JOIN status ON projects.proj_status_id = status.id
GROUP BY
proj_start_date

最佳答案

听起来您必须查看“LEFT JOIN”https://www.w3schools.com/sql/sql_join_left.asp否则你会错过绿色圆圈的左侧部分。

关于mysql - SQL AND 无要求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51025541/

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