简体   繁体   中英

PHP take id's from one table and pull data that corresponds with those id's from another table

I have this query:

$query = "SELECT *FROM wp_postmeta WHERE meta_key = '_isEvent' AND meta_value ='yes' ORDER BY post_id LIMIT 0, 5" or die(mysql_error()); 
    $result = mysql_query($query) or die(mysql_error());

    while ($row = mysql_fetch_array($result)) { 

        $eventid = $row ['post_id'];

    echo "<p>".$eventid."</p>";


    }

This currently posts some ids $eventid . I now want to run another query in another table (same database) that pulls some post titles that matches those id's.

Table is called "wp_posts" and column to match is "ID" and want to echo out post title from "post_title".

Where do I start?

Sounds like you want to use SQL joins. W3 schools has a good intro article about this. http://www.w3schools.com/sql/sql_join.asp

Something like the following

SELECT post_title FROM wp_postmeta m, wp_posts p WHERE m.wmeta_key = '_isEvent' AND m.meta_value ='yes' AND m.post_id == p.post_idORDER BY m.post_id LIMIT 0, 5

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