views:

62

answers:

2

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']; }
A: 
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'
thetaiko
+1  A: 

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'
Daniel Vandersluis
Hmm, get no errors but cant get it to work as intended. Should I be able to this still? $user_id = $row['tbl_row_id'];
Greenie
No, cant get it to work. I think its because I need to look in wp_pods_rel 2 times. One time when I have the wp.ID to get the wpr.pod_id and then use that to fine another entry with the same wpr.pod_id where wpr.field_id = '28'
Greenie
What is the intended result?
Daniel Vandersluis
to get the wpr.tbl_row_id where wpr.field_id = '28'
Greenie
Im really trying to understand this, tried to explain it better in another question:http://stackoverflow.com/questions/2504273/help-with-grasping-inner-join
Greenie
Got it now, you helped me get on track. Thanks! This is what I ended up with (with help from the other question):SELECT guid, second.tbl_row_id AS user_id FROM wp_posts INNER JOIN wp_pod_rel AS first ON wp_posts.ID=first.tbl_row_id INNER JOIN wp_pod_rel AS second ON first.pod_id=second.pod_id WHERE second.field_id='28'
Greenie