PHP MySQL LEFT JOIN not working

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.

Read More

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

        ");

    }

Related posts

1 comment

  1. Look at your query and see that JOINS are total weired. Modify your query like below

        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;
    

Comments are closed.