PHP MySQL左连接不工作
我想知道是否有人能帮我解决这个问题PHP MySQL左连接不工作,php,mysql,wordpress,web,left-join,Php,Mysql,Wordpress,Web,Left Join,我想知道是否有人能帮我解决这个问题 Example Table ($tableTerms): ----------------------------- | NAME | SLUG | ----------------------------- | Dean | dean | ----------------------------- | Football | football | -----------------------
Example Table ($tableTerms):
-----------------------------
| NAME | SLUG |
-----------------------------
| Dean | dean |
-----------------------------
| Football | football |
-----------------------------
我试图从$tableTerms表中的“name”列中获取一些数据
在查询过程中,我将列“slug”调用为,以便查找“Dean”参加的正确的“event_name”。然后在同一个查询中,我想调用列(“name”),但这次访问行“Football”,这样我就可以看到“Dean”参加了什么类型的活动
我使用Wordpress的术语和分类法,这就是为什么在同一个表中有不同类型的数据
我试图用左联来达到我的目标,但是我没有运气。在我添加左连接和额外的SELECT之前,查询工作正常,但我现在需要额外的数据
任何人的帮助都会很好,谢谢
迪恩
查看您的查询,并查看
联接
是否完全正确。如下所示修改您的查询
SELECT
event_name,
event_start_date,
location_name,
tableTermsCategory.name
FROM
$tableTerms LEFT JOIN $tableTermTaxonomy ON $tableTerms.term_id = $tableTermTaxonomy.term_id
AND $tableTermTaxonomy.taxonomy IN ('event-categories', 'event-tags')
AND $tableTermTaxonomy.term_taxonomy_id = $tableTermRelationships.term_taxonomy_id
LEFT JOIN $tableTermRelationships ON $tableTermRelationships.term_taxonomy_id = $tableTermTaxonomy.term_taxonomy_id
LEFT JOIN $tableEvents ON $tableEvents.post_id = $tableTermRelationships.object_id
LEFT JOIN $tableEventLocations ON $tableEvents.location_id = $tableEventLocations.location_id
WHERE
YEAR(event_start_date) = $year AND
slug = '$slug'
ORDER BY event_start_date;
为什么您有来自$tableterms的
。。。左连接$tableterms
?你不应该这样混合连接语法。选择一种风格并坚持下去。你的查询只是个坏人,试图用from,bad…bad…bad,到底是什么错误?mysqlI返回的agree with@AlexandreCarvalhodeMelo查询非常糟糕。使用$tableTerms、$tableTermTaxonomy、$tableTermRelationships、$tableEvents、$tableEventLocations中的4个内部联接,并在未设置字段的情况下使用,似乎您对数据库架构一无所知,并要求mysql服务器猜测它是什么。
SELECT
event_name,
event_start_date,
location_name,
tableTermsCategory.name
FROM
$tableTerms LEFT JOIN $tableTermTaxonomy ON $tableTerms.term_id = $tableTermTaxonomy.term_id
AND $tableTermTaxonomy.taxonomy IN ('event-categories', 'event-tags')
AND $tableTermTaxonomy.term_taxonomy_id = $tableTermRelationships.term_taxonomy_id
LEFT JOIN $tableTermRelationships ON $tableTermRelationships.term_taxonomy_id = $tableTermTaxonomy.term_taxonomy_id
LEFT JOIN $tableEvents ON $tableEvents.post_id = $tableTermRelationships.object_id
LEFT JOIN $tableEventLocations ON $tableEvents.location_id = $tableEventLocations.location_id
WHERE
YEAR(event_start_date) = $year AND
slug = '$slug'
ORDER BY event_start_date;