[英]PHP MySQL LEFT JOIN not working
我想知道是否有人能帮我解决这个问题。
Example Table ($tableTerms):
-----------------------------
| NAME | SLUG |
-----------------------------
| Dean | dean |
-----------------------------
| Football | football |
-----------------------------
我正在尝试从表$ tableTerms中的“name”列中获取一些数据。
在查询期间,我将列调为“slug”,以便查找“Dean”参加的正确“event_name”。 然后在同一个查询中我想调用列(“名称”),但这次访问行“足球”,这样我就可以看到“Dean”参加的事件类型。
我正在使用Wordpress的术语和分类法,这就是为什么同一个表中有不同类型的数据。
我试图用LEFT JOIN实现我的目标,但我没有运气。 在我添加LEFT JOIN和额外的SELECT之前查询工作,但我现在需要额外的数据。
任何人的帮助都会很棒,谢谢!
院长。
// Look Up Events Attended
$tableTerms = $wpdb->prefix . "terms";
$tableTermTaxonomy = $wpdb->prefix . "term_taxonomy";
$tableTermRelationships = $wpdb->prefix . "term_relationships";
$tableEvents = $wpdb->prefix . "em_events";
$tableEventLocations = $wpdb->prefix . "em_locations";
$tableEventsMeta = $wpdb->prefix . "em_meta";
$slug = strtolower($firstName)."-".strtolower($lastName);
if(isset($memberID)) {
$eventsAttendedQuery = $wpdb->get_results("
SELECT
event_name,
event_start_date,
location_name,
tableTermsCategory.name
FROM
$tableTerms,
$tableTermTaxonomy,
$tableTermRelationships,
$tableEvents,
$tableEventLocations
LEFT JOIN
$tableTerms AS tableTermsCategory
ON tableTermsCategory.term_id = $tableTermTaxonomy.term_id AND
$tableTermTaxonomy.taxonomy = 'event-categories' AND
$tableTermTaxonomy.term_taxonomy_id = $tableTermRelationships.term_taxonomy_id AND
$tableTermRelationships.object_id = $tableEvents.post_id
WHERE
YEAR(event_start_date) = $year AND
slug = '$slug' AND
$tableTerms.term_id = $tableTermTaxonomy.term_id AND
$tableTermTaxonomy.taxonomy = 'event-tags' AND
$tableTermRelationships.term_taxonomy_id = $tableTermTaxonomy.term_taxonomy_id AND
$tableEvents.post_id = $tableTermRelationships.object_id AND
$tableEvents.location_id = $tableEventLocations.location_id
ORDER BY event_start_date ASC
");
}
查看您的查询,看看JOINS
是否已经完成了。 修改您的查询,如下所示
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.