dongshi1606 2012-09-11 21:02
浏览 26
已采纳

基于$ get值的mysql join

I have 2 tables.

contacts and complaints.

contacts has 3 columns:

contact_id, contact_name, score

complaints has 4 columns:

complaint_id, contact_id, score_deduction, complaint_type

I am able to obtain value $_GET['complaint_id'] from previous page.

What i am trying to do is delete the record with that particular complaint_id and get the value of complaints.score_deduction and add it back to contacts.score.

I have spent hours and cant seem to figure it out.

Please help.

if (isset($_GET['complaint_id'])) {
record_set('complaints',"SELECT * FROM add_complaints WHERE complaint_id = ".$_GET['complaint_id']."");
$contact_id = $row_complaints['contact_id'];
record_set('contact',"SELECT * FROM contacts WHERE contact_id = $contact_id");
$score = $row_contact['contact_score'];
echo $score;

}
  • 写回答

1条回答 默认 最新

  • drws65968272 2012-09-11 21:31
    关注

    You can just combine that into a single query.

    SELECT * FROM add_complaints AS ac
    INNER JOIN contacts AS c on ac.contact_id = c.contact_id
    WHERE ac.complaint_id = ?
    

    I am not familiar with whatever you are using to make the DB queries. But I assume you would know how to get the contact id, current score, and score deduction values. And make the update query and delete queries from there.

    IF you wanted to get more fancy and just update the contact record in place without needing to select anything you could also do:

    UPDATE add_complaints AS ac
    INNER JOIN contacts AS c on ac.contact_id = c.contact_id
    SET c.score = c.score - ac.score_deduction
    WHERE ac.complaint_id = ?
    

    Then just delete the complaint

    DELETE FROM add_complaints WHERE complaint_id = ?
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥25 关于##爬虫##的问题,如何解决?:
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误