I was wondering if anyone would be able to help me out with this problem.
Example Table ($tableTerms):
-----------------------------
| NAME | SLUG |
-----------------------------
| Dean | dean |
-----------------------------
| Football | football |
-----------------------------
I’m trying to get some data from the column “name” in the table $tableTerms.
During the query I call the column “slug” to so I can lookup the correct “event_name” which “Dean” attended. Then in the same query I want to call the column (“name”) but this time access the row “Football” so I can see what type of event “Dean” was attending.
I’m using WordPress’s Terms and Taxonomies which is why there is different types of data in the same table.
I have tried to achieve my goal using LEFT JOIN but I’m having no luck. The query worked before I added the LEFT JOIN and the extra SELECT but I now need the additional data.
Any help from anyone would be great, thanks!
Dean.
// 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
");
}
Look at your query and see that
JOINS
are total weired. Modify your query like below