如何使用PHP或MySQL合并MySQL数据库中具有2个相同标识符和2个唯一标识符的行

时间:2018-09-02 12:25:11

标签: php mysql

好的,这是我的第一个问题,我真的不知道该怎么问,所以我将尝试并尽可能具体。我的网站是在线游戏,在将新商品插入数据库时​​用于用户库存

Table name "inventory" 
Column names "inv_id", "inv_itemid", "inv_userid", "inv_qty" 

,它不会添加到列inv_qty并正确填充,而是为每个项目创建一个新的inv_id标识符和行。我想知道是否有办法通过php创建合并函数,以将所有具有相同inv_itemid和inv_userid的项合并,同时添加到inv_qty列并填充inv_id

在我的inventory.php文件中,inv_id列用于让用户装备该项或将其用作主要变量。

我已经看过了,并且尝试了很多次,但我无法使其正常工作。

2 个答案:

答案 0 :(得分:0)

如果要检查的是单个键,则可以使用mysql的“ ON DUPLICATE KEY UPDATE”,如下所示:

class GFG {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while(t>0){
            int n = sc.nextInt();
            int count = 0;
            String str = sc.next();
            String strArray[] = str.split(" ");
            HashMap <String,Integer> wordCount = new HashMap<String,Integer>();
            for(int i=0;i<n;i++){
                if(wordCount.containsKey(strArray[i])){
                    wordCount.put(strArray[i],wordCount.get(strArray[i])+1));
                }
                else{
                     wordCount.put(strArray[i],1);
                }
            }

            for (Map.Entry<String, Integer> entry : wordCount.entrySet()) {
                if(entry.getValue()==2)
                    count++;
            }

            System.out.println(count);
            t--;

        }
    }
}

但是在您的情况下,需要综合考虑。 如果“ inv_id”,“ inv_itemid”,“ inv_userid”可以计算,则为UPDATE,否则为INSERT。

在单个查询中仅使用mysql来实现此目的的一种方法是创建和使用存储过程。

但是使用php可以在2个查询中实现。第一个查询是确定组合是否存在。然后基于此运行下一个“插入”或“更新”查询。 请检查以下示例:

INSERT INTO table(field1, field2, field3, ..)
 VALUES (val1, val2, val3, ...)
ON DUPLICATE KEY 
 UPDATE field3='*'

如果结果为空,然后插入:

$sql1 = SELECT * FROM inventory WHERE inv_id='$inv_id', inv_itemid='$inv_itemid', inv_userid='$inv_userid'
// Execute $sql1 and get the result.

否则UPDATE。

$sql2 = INSERT INTO inventory ....

答案 1 :(得分:0)

关于:

  

是否可以在清单页面顶部编写一个php函数供我的用户单击以合并它们

请检查以下php函数。

通过使用param:UserID调用,它将为每个(inv_itemid + inv_userid)组合创建一个总inv_qty总和的新条目,并删除(inv_itemid + inv_userid)以前的重复条目,并保留新输入的内容:(inv_itemid + inv_userid +(inv_qty之和)。

重要提示,请在运行该功能之前备份数据库表数据。

请检查功能中的注释并根据您的系统进行必要的更新,就像获取最后插入的inv_id一样。

function merger_fnc($user_id) {
    // For Each Combination of: inv_itemid + inv_userid
    // This function will Insert a new row in the inventory with the SUM of inv_qty 
    // And then will remove the previous single rows of: inv_itemid + inv_userid + inv_qty

    // First get the distinct Items of the User(by UserID);
    $inv_itemids = $db->query("SELECT DISTINCT(inv_itemid) FROM inventory WHERE inv_userid=".$user_id);
    // Here $inv_itemids will hold all the distinct ItemIDs for the UserID;

    foreach ($inv_itemids as $inv_item) {
        // We will Insert A new row which will have the sum of 'inv_qty' for the inv_userid & inv_itemid;
        $inv_itemid = $inv_item['inv_itemid'];
        // I am not sure what type of result set your $db->query(...) returns. So I assumed it is associative array. 
        // If the result is an Array of objects, then please use: $inv_itemid = $inv_item->inv_itemid;

        $insert_sql = "INSERT INTO inventory (inv_itemid, inv_userid, inv_qty) VALUES ('".$inv_itemid."', '".$user_id."', (SELECT SUM(inv_qty) FROM FROM inventory WHERE inv_userid=".$user_id."))";
        $inv_itemids = $db->query($insert_sql);

        $inserted_new_inventory_id = $db->insert_id;
        // Please check the appropriate method for it in your $db class here.
        // In mysqli, it is: mysqli_insert_id($db_conn); In PDO it is: $db_conn->lastInsertId();

        // Last we remove the previous data of combination(inv_userid & inv_itemid) but leaving our last inserted row.
        $delete_sql = "DELETE FROM inventory WHERE inv_id!='".$inserted_new_inventory_id."' AND inv_userid='".$user_id."' AND inv_itemid='".$inv_itemid."'";
        $db->query($delete_sql);
    }
}

如果从$ db获取最后插入的inv_id是麻烦的(例如inv_id未在表中定义为键),则可以尝试另一种方法:

再次执行查询,然后在插入之前将先前的inv_id保存在数组中。 插入总数量为新的条目后,运行删除查询以删除先前的单个数量条目,如下所示:

DELETE FROM inventory WHERE inv_id IN (3, 4, 7,...)
  • 以下(3、4、7,...)是(inv_itemid + inv_userid)组合的先前inv_id。