Search code examples
mysqldatabasejoindrupal-7inner-join

drupal7 get content with manual sql


Im trying to get all the articles from the drupal7 table using manual sql method, I am trying to use the tables named field_data_body, node, file_usage, file_managed. What I really want is something like this:

body_value | body_summary | nid | title | created_time | uri

I used the following sql code:

SELECT distinct body.body_value, body.body_summary, node.nid, node.title, FROM_UNIXTIME(node.created) as created, file_managed.uri
            FROM 
                node 
                    INNER JOIN field_data_body as body ON node.nid = body.entity_id
                    INNER JOIN file_managed ON file_usage.fid = file_managed.fid
            WHERE body.entity_id = node.nid
                AND node.type = 'article'
                AND node.status = '1'
            GROUP BY node.nid
            ORDER BY node.nid desc

it worked but I got only those articles that has an image with it and all the rest got ignored. Can someone suggest me on how to deal with these sql conditions?


Solution

  • SELECT
      b.body_value,
      b.body_summary,
      n.nid,
      n.title,
      n.created,
      fm.uri
    FROM
    node n
    LEFT JOIN field_data_body AS b ON n.nid = b.entity_id
    LEFT JOIN file_usage AS fu ON fu.id = n.nid
    LEFT JOIN file_managed AS fm ON fm.fid = fu.fid
    WHERE n.type = 'article'
      AND n.status = '1'
    GROUP BY n.nid
    ORDER BY n.nid DESC;