donglun2024 2015-08-09 11:30
浏览 117
已采纳

从一个表中选择id在另一个表中,并用不同于第二个表的char替换整数

I have two tables, one is a user log which stores the user by number

timestamp / user_id / transaction_id / amount

the other is a user table which has the users number and their full name

user_id / fullname

I want to select the entire user log and display it, but instead of displaying their number, display their full name from the other table, but I can't get it working. I keep modifying the sql and breaking it. Is there a way to accomplish this with php postgresql or should I use a function? I keep getting an error that user_id is integer and the fullname is not Please assist.

    $query = "SELECT * FROM user_log 
    INNER JOIN user_staff
ON user_log.user_id=user_staff.user_name
ORDER BY user_log_id DESC LIMIT 200;";

    $result = pg_query($query); 
    if (!$result) { 
        echo "Problem with query " . $query . "<br/>"; 
        echo pg_last_error(); 
        exit(); 
    } 

    while($myrow = pg_fetch_assoc($result)) { 
        printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>", $myrow['timestamp'], htmlspecialchars($myrow['user_id']), htmlspecialchars($myrow['transaction_id']), htmlspecialchars($myrow['amount']));
    } 
    ?> 
  • 写回答

2条回答 默认 最新

  • douyi2664 2015-08-09 11:43
    关注

    Perhaps something like:

    SELECT user_log.timestamp, users.fullname, user_log.transaction_id, user_log.amount
    FROM user_log
    INNER JOIN users
    ON users.user_id=user_log.user_id 
    ORDER BY user_log_id 
    DESC LIMIT 200;
    

    You can read up on SQL Joins here: http://www.w3schools.com/sql/sql_join.asp

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料