dongyanju0945 2014-01-19 03:07
浏览 19

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.
  • 写回答

2条回答 默认 最新

  • dongyimeng3764 2014-01-19 03:27
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计