[英]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.