WordPress Custom SQL Query and Group Concat

I am trying to run the below query to get the unique result of object_id. I figured using Group_concat Distinst might work the best. I need to used the results of the query as a variable in the where clause of my next query.

The below query runs fine in flyspeedsql software, but I can’t seem to see the results in php.

Read More

Is there a better way to approach this?

Is wordpress get_results the right function to use?

It should return something link 55,56,57

Also from the results of this query how do I set up a variable that could be used in the where clause of my next query?

Query 1

      $query = ("Select
         Group_Concat(Distinct $wpdb->term_relationships.object_id)
      From
           $wpdb->term_taxonomy Inner Join
           $wpdb->term_relationships On $wpdb->term_taxonomy.term_taxonomy_id =
           $wpdb->term_relationships.term_taxonomy_id Inner Join
           $wpdb->terms On $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
       Where
           $wpdb->terms.name = 'Honda' And
           $wpdb->term_taxonomy.taxonomy = 'brands'");

       $results = $wpdb->get_results($query);

Related posts

Leave a Reply

1 comment

  1. Have you heard of MYSQL sub queries?

    Honestly, I haven’t read that whole article myself, but I know the concept well enough to do what you want with 1 query (though it’ll be monstrous)

    Select
         $wpdb->some_other_table.some_field
    From
         $wpdb->some_other_table
    Where
         $wpdb->some_other_table.some_other_field = (Select
           Group_Concat(Distinct $wpdb->term_relationships.object_id)
      From
           $wpdb->term_taxonomy Inner Join
           $wpdb->term_relationships On $wpdb->term_taxonomy.term_taxonomy_id =
           $wpdb->term_relationships.term_taxonomy_id Inner Join
           $wpdb->terms On $wpdb->term_taxonomy.term_id = $wpdb->terms.term_id
       Where
           $wpdb->terms.name = 'Honda' And
           $wpdb->term_taxonomy.taxonomy = 'brands');
    

    I don’t know if this’ll fit your needs 100% because your description is rather vague, but I’m sure you can figure out how to make this work for you if it turns out being a solution.