简体   繁体   中英

SQL Query function and joining databases

I would like to get all the the corresponding images from one mySQL table that have a corresponding post_ID to the ID of another table.

So with an ID of 7 in table wp_post, I want to echo the values of infomation in another table that has a certain meta_key and a corresponding ID.

===WP_POST===========       ===WP_POSTMETA======================================
ID     ||     content       post_id     ||     meta_key     ||     meta_value
================================================================================

The meta_key that I want is listed as _wp_attached_file in the database.

So I want to get all meta_values of wp_postmeta table that have a meta_key of _wp_attached_file and a post_id that matches the current ID of the page being viewed from the table wp_posts .

So obviously the tables will need to be joined but I need a hand.

Any ideas,

Marvellous AHAA

Implemented solution below and I am now receiving an error.

<?php
$pics = mysql_query("SELECT t2.meta_key,t2.meta_value from (select id from wp_post) t1
join wp_postmeta t2 on (t2.post_id=t1.postid)
where t2.meta_key='_wp_attached_file'");

while ($row = mysql_fetch_array($pics)) {
    $thumb = $row['meta_value'];
    echo '<img src="http://www.golfbrowser.com/wp-content/uploads/'.$thumb.'" />';

}
?>

Any ideas?

I tried a simpler way myself. Still does not work.Can anyone see what is wrong.

<?php

$post_id = $post->ID;
$pics = mysql_query("

        SELECT meta_value * FROM wp_postdata 
        WHERE meta_key ='_wp_attached_file'
        AND post_id = '%$post_id%'
");

while ($row = mysql_fetch_array($pics)) {
    $thumb = $row['meta_value'];
    echo '<img src="http://www.golfbrowser.com/wp-content/uploads/'.$thumb.'" />';

}
?>

Try this query:

SELECT A.meta_key
FROM WP_POSTMETA AS A
    JOIN WP_POST AS B
        ON (A.post_id = B.ID)
WHERE A.post_id='%post_id%' ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM