0

我有一个查询,我需要访问由 ID 相关的不同表中的信息以获取必要的信息。这就是代码的样子,我想知道是否有更有效的方法来编写代码?

<?php 
global $wpdb;
$eventlist = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_events_detail LIMIT 3 ", $id ), OBJECT );
foreach ( $eventlist as $event ) {
    $get_team_id = $event->id;
    $startdate = $event->start_date;
    $enddate = $event->end_date;
    echo  '<div class="span3">';
    echo  '<h3>' .$event->event_name. '</h3>';
    echo ''.date("l", strtotime($startdate)).'<br />';
    echo date("F", strtotime($startdate));
    echo $event->venue_title;

    /* GET START AND END TIME */
        $event_times = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_events_start_end WHERE event_id = %d", $get_team_id ), OBJECT );
        foreach ( $event_times as $event_time ) {
            $start_time = $event_time->start_time;
            $end_time   = $event_time->end_time;

            echo '<strong>' . date("g:i", strtotime($start_time)) . '</strong><small>' . date("a", strtotime($start_time)) . '</small> > ';
            echo '<strong>' . date("g:i", strtotime($end_time)) . '</strong><small>' . date("a", strtotime($end_time)) . '</small>';
        }

        /* Get Venue Name */
        $event_venue = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_events_venue_rel WHERE event_id = %d", $get_team_id ), OBJECT );
        foreach ( $event_venue as $venue ) {
            $venue_id = $venue->venue_id;                               
            $ts = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_events_venue WHERE id = %d", $venue_id ), OBJECT );
            foreach ( $ts as $t ) {
                echo $t->name;
            }
        }
    echo  '</div>';
}
?>
4

1 回答 1

0

是的,有一种有效的方法。在此处此处此处查看 MySQL 中的 JOIN

于 2013-02-02T13:48:27.600 回答