I am trying to convert the following SQL query in calls to the database API.
SELECT p.id, p.message,t.posted, p.posted AS reply_date,p.poster_id,t.id,t.subject, t.forum_id FROM punbb.punbb_posts AS p JOIN punbb.punbb_topics AS t ON p.topic_id = t.id WHERE p.id = t.first_post_id;
What I get is the following code.
$query = db_select('punbb_posts', 'p')
->fields('p', array('id', 'message', 'posted', 'poster_id'));
$query->join('punbb_topics', 't', 'p.topic_id = t.id');
$query->addField('t', 'posted');
$query->addField('t', 'id');
$query->addField('t', 'subject');
$query->addField('t', 'forum_id');
$query->addField('t', 'first_post_id');
$query->condition('t.first_post_id','p.id');
The code generates the following SQL query.
SELECT p.id AS id, p.message AS message, p.posted AS posted, p.poster_id AS poster_id, t.posted AS t_posted, t.id AS t_id, t.subject AS subject, t.forum_id AS forum_id, t.first_post_id AS first_post_id
FROM
{punbb_posts} p
INNER JOIN {punbb_topics} t ON p.topic_id = t.id
WHERE (t.first_post_id = :db_condition_placeholder_0)
Why can't I get "PID"?
$query->join('punbb_topics', 't', 'p.topic_id = t.id AND t.first_post_id = p.id');
– Paul Nov 11 '14 at 8:10