dsfdfd1211 2015-06-26 09:17
浏览 86
已采纳

如何在PHP MYSQL中关联两列

I have created one request table that contain all request information.

The table containing these columns:

requestid , itemrequest1, itemrequest2, itemrequest3, quantity1, quantity2, quantity3

How to make a relation so that if the itemrequest has a value in column itemrequest2, it will take the quantity from column quantity2.

My query is like this:

$query2=mysql_query("select * from tbl_request WHERE unit='$unit' AND  (itemrequest1='$itemrequest' or itemrequest2='$itemrequest' or  itemrequest3='$itemrequest')");
$record_num=mysql_num_rows($query2);

while ($data1 = mysql_fetch_array($query2))

The problem is that when the $itemrequest has a value in column itemrequest3, the quantity will always show the quantity from column 1.

  • 写回答

1条回答 默认 最新

  • duanbei1709 2015-06-26 09:30
    关注

    You can use conditions in your query in order to select the right column depending on another column's value.

    SELECT requestid, 
        (CASE WHEN itemrequest1 = '$itemrequest' THEN quantity1
            ELSE (
                CASE WHEN itemrequest2 = '$itemrequest' THEN quantity2
                ELSE (
                    CASE WHEN itemrequest3 = '$itemrequest' THEN quantity3
                END)
            END)
        END)
    FROM tbl_request
    

    You should have a close look to your database structure though, if you have to do things like this, it might be better to improve your structure and have better tables/fields.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA