Question

If you are familiar with Drupal this is using 2 taxonomy terms to describe each content_type_event node. If you don't know Drupal you still have everything below.

I have built a SQL Fiddle that is easier to follow and test with than my drawn out tables. The fiddle has the actual database content which is a little different than the sample info that is shown below but I have tried to make them as similar as possible.

I have three tables:

content_type_event:

____________________________________
| nid | field_eventstartdate_value |
------------------------------------
|  17 | 1984581600                 |
|  18 | 1984581600                 |
|  19 | 1984581600                 |
|  20 | 1984581600                 |
|  22 | 1984581600                 |
====================================

term_node:

_____________
| nid | tid |
-------------   
|  17 |   6 |   
|  17 |  15 |
|  18 |   7 |  
|  18 |  17 |
|  19 |   6 |
|  19 |  15 |
|  20 |  16 |
|  20 |   9 |
|  22 |  10 |
|  22 |  15 |
=============

term_data:

__________________________
| tid | vid |    name    |
--------------------------
|   6 |  4  | Location 1 |
|  15 |  3  |    Event 1 |
|   7 |  4  | Location 2 |
|   9 |  4  | Location 3 |
|  10 |  4  | Location 4 |
|  16 |  3  |    Event 2 |
|  17 |  3  |    Event 3 |
==========================

The content_type_event table has information about the event but for the location and event type I have to dig deeper.

The term_node table has all the tid (term id) that goes to each nid (node id). Each nid should have 2 tid for our events. One tid will give us our location the other will give use our event_type.

The term_node table gives the name of the tid and gives us a vid that tells if this name is an event_type or location.

My goal is to get the nid, location, event_type, field_eventstartdate_value for all events. All of the following start in the future so it should look like:

______________________________________________________________
| nid |  location  | event_type | field_eventstartdate_value |
--------------------------------------------------------------
| 17  | Location 1 |   Event 1  |                 1984581600 |
| 18  | Location 2 |   Event 3  |                 1984581600 |
| 19  | Location 1 |   Event 1  |                 1984581600 |
| 20  | Location 3 |   Event 2  |                 1984581600 |
| 22  | Location 4 |   Event 1  |                 1984581600 |
==============================================================

I am not so good with SQL. This is what I have so far:

SELECT event.nid, event.field_eventstartdate_value, location.name, event_type.name
FROM content_type_event AS event 
JOIN term_node ON term_node.nid = event.nid
LEFT JOIN (
  SELECT tid, name AS location FROM term_data WHERE vid = 4
) AS location ON location.tid = term_node.tid
LEFT JOIN (
  SELECT tid, name AS location FROM term_data WHERE vid = 3
) AS event_type ON event_type.tid = term_node.tid;

But this gives me:

______________________________________________________________
| nid |  location  | event_type | field_eventstartdate_value |
--------------------------------------------------------------
| 17  |       NULL |   Event 1  |                 1984581600 |
| 17  | Location 1 |      NULL  |                 1984581600 |
| 18  |       NULL |   Event 3  |                 1984581600 |
| 18  | Location 2 |      NULL  |                 1984581600 |
| 19  |       NULL |   Event 1  |                 1984581600 |
| 19  | Location 1 |      NULL  |                 1984581600 |
| 20  |       NULL |   Event 2  |                 1984581600 |
| 20  | Location 3 |      NULL  |                 1984581600 |
| 22  |       NULL |   Event 1  |                 1984581600 |
| 22  | Location 4 |      NULL  |                 1984581600 |
==============================================================

I can not seem to group these results together so I get just one full row per event instead of 2 rows containing partial info.

If you want them you can grab the table build statements off of the

FIDDLE: SQL Fiddle.

Was it helpful?

Solution

I think you need this:

SELECT
  event.nid,
  location.location,
  event_type.event_type,
  event.field_eventstartdate_value
FROM
  content_type_event AS event
LEFT OUTER JOIN (
   SELECT
      nid,
      name AS location
    FROM
      term_data JOIN
      term_node ON term_data.tid=term_node.tid
    WHERE term_data.vid = 4) AS location ON location.nid = event.nid
LEFT OUTER JOIN (
   SELECT
      nid,
      name AS event_type
    FROM
      term_data JOIN
      term_node ON term_data.tid = term_node.tid
    WHERE term_data.vid = 3) AS event_type ON event_type.nid = event.nid;

http://sqlfiddle.com/#!2/c2459/17/0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top