PHP。 如何从2个mysql表而不是1中获取数据

Learning php and I am losing my mind trying to solve this for days now. Please help.

  1. This is a code which goes thought a table COUPON, take data with a condition met, and download it afterwards. In this table COUPON I have USER_ID as number but I want to have a user name also, which is kept in another table USER.

How can I go to another table (USER) and take names (REALNAME) by this USER_ID which is the same in both tables?

if ( $_POST ) {
    $team_id = abs(intval($_POST['team_id']));
    $consume = $_POST['consume'];
    if (!$team_id || !$consume) die('-ERR ERR_NO_DATA');

    $condition = array(
        'team_id' => $team_id,
        'consume' => $consume,
    );

    $coupons = DB::LimitQuery('coupon', array(
        'condition' => $condition,
    ));

    if (!$coupons) die('-ERR ERR_NO_DATA');
    $team = Table::Fetch('team', $team_id);
    $name = 'coupon_'.date('Ymd');
    $kn = array(
        'id' => 'ID',
        'secret' => 'Password',
        'date' => 'Valid',
        'consume' => 'Status',
        );

    $consume = array(
        'Y' => 'Used',
        'N' => 'Unused',
    );
    $ecoupons = array();
    foreach( $coupons AS $one ) {
        $one['id'] = "#{$one['id']}";
        $one['consume'] = $consume[$one['consume']];
        $one['date'] = date('Y-m-d', $one['expire_time']);
        $ecoupons[] = $one;
    }
    down_xls($ecoupons, $kn, $name);
  1. After this, I want to try to do the same thing using only SQL queries.

You would need to JOIN the tables in the SQL query

SELECT something FROM coupons as coupons JOIN user as user ON coupons.id=user.id

You should use join when you want to retrieve details from two tables. Join table COUPON and table USER based on user_id . This should yield results you want.