更新SQL中的表,WHERE子句多个字段

时间:2016-10-22 16:12:38

标签: mysql sql

您好我想用tblrestocklog及其相应的stockno更新我的productno

这是我的样本表:

StockNo     ProductNo   Quantity    PurchasedDate   ExpirationDate
1017123002  25373          10         2016-10-22    2017-02-10
1017123003  25370          10         2016-10-22    2018-11-21
1017123006  25370          10         2016-10-22    2018-03-30
1017123005  25370          10         2016-10-22    2018-04-22

现在我想用最小的库存号1017123003更新Product 25370。

我尝试了这个查询和其他东西,但它总是给我一个错误信息..

UPDATE tblrestocklog 
    SET quantity = 20
    WHERE MIN(stockno) AND productno = 25370;

3 个答案:

答案 0 :(得分:2)

您可以在window.onload = createList; function addEvent(object, evName, fnName, cap) { if (object.attachEvent) object.attachEvent("on" + evName, fnName); else if (object.addEventListener) object.addEventListener(evName, fnName, cap); } function uniqueElemText(elemName) { elems = document.getElementsByTagName(elemName); elemsArray = new Array(); for (var i=0; i<elems.length; i++) elemsArray[i]=elems[i].innerHTML; elemsArray.sort(); for (i=0; i<elemsArray.length-1; i++) { if (elemsArray[i]==elemsArray[i+1]) { elemsArray.splice(i+1,1); i--; } } return elemsArray; } function createList(){ var option; var characters = new Array(); var select = document.createElement('select'); select.setAttribute('id', 'charList'); select.addEventListener('change', filter, false); var charBox = document.getElementById('characterList'); charBox.innerHTML = '<p>Show Only Line By:</p>'; charBox.appendChild(select); var tempChar = document.getElementsByTagName('h3'); for(var i = 0; i < tempChar.length; i++) if(characters.indexOf(tempChar[i].innerHTML) == -1) characters.push(tempChar[i].innerHTML); delete tempChar; characters.sort(); option = document.createElement("option"); option.text = "Show All Character Lines"; option.value = 'ALL'; select.add(option); for(var i = 0; i < characters.length; i++) { option = document.createElement("option"); option.text = characters[i]; option.value = characters[i]; select.add(option); } } function filter(){ var scene = document.getElementById('scene'); var elemList = scene.getElementsByTagName('*'); var list = document.getElementById('charList'); var character = list.options[list.selectedIndex].value; var hide = false; for (var i = 0; i < elemList.length; i++) { var elem = elemList[i]; if (elem.tagName == "H3") { hide = (elemList[i].innerHTML.indexOf(character) == -1) } if (hide) { elem.style.opacity = 0.0; } else { elem.style.opacity = 1.0; } } } 声明中使用order bylimit

update

答案 1 :(得分:1)

这是另一种方法

UPDATE tblrestocklog A
       JOIN (SELECT Min(stockno) min_stockno,
                    productno
             FROM   tblrestocklog p
             GROUP  BY productno) B
         ON A.productno = B.productno
            AND A.stockno = B.min_stockno
SET    quantity = 20
WHERE  productno = 25370 

您可以从productno子句中删除where过滤器,以便为所有productno

应用此逻辑

答案 2 :(得分:0)

尝试以下查询:

UPDATE tblrestocklog 
SET quantity = 20
WHERE  productno = 25370
AND stockno IN (SELECT MIN(stockno))