ugreen
ugreen

Reputation: 672

Is it possible to combine these 3 mySQL queries?

I know the $downloadfile - and I want the $user_id. By trial and error I found that this does what I want. But it's 3 separate queries and 3 while loops. I have a feeling there is a better way. And yes, I only have a very little idea about what I'm doing :)

$result = pod_query("SELECT ID FROM wp_posts WHERE guid LIKE '%/$downloadfile'");
while ($row = mysql_fetch_assoc($result)) { 
    $attachment = $row['ID']; }

$result = pod_query("SELECT pod_id FROM wp_pods_rel WHERE tbl_row_id = '$attachment'");
while ($row = mysql_fetch_assoc($result)) {
    $pod_id = $row['pod_id']; }

$result = pod_query("SELECT tbl_row_id FROM wp_pods_rel WHERE tbl_row_id = '$pod_id' AND field_id = '28'");
while ($row = mysql_fetch_assoc($result)) {
    $user_id = $row['tbl_row_id']; }

Upvotes: 1

Views: 170

Answers (2)

Daniel Vandersluis
Daniel Vandersluis

Reputation: 94153

Assuming I am understanding your queries correctly, this should work:

SELECT wp.ID, wpr.pod_id, wpr.tbl_row_id
FROM wp_pods_rel AS wpr
JOIN wp_posts AS wp
  ON wp.ID = wpr.tbl_row_id
WHERE wpr.field_id = '28'
  AND wp.guid LIKE '%/$downloadfile'

Upvotes: 1

thetaiko
thetaiko

Reputation: 7834

SELECT wp_posts.ID, wp_pods_rel.pod_id, wp_pods_rel.tbl_row_id
FROM wp_posts
JOIN wp_pods_rel ON wp_posts.ID = wp_pods_rel.tbl_row_id
WHERE wp_posts.guid LIKE '%/$downloadfile' AND wp_pods_rel.field_id = '28'

Upvotes: 0

Related Questions