Fw: [程式] Update裡面進行子查詢失敗( 已得另一解 )

看板GameDesign作者 (芋頭一顆多少錢)時間7年前 (2016/12/13 17:44), 7年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
※ [本文轉錄自 Database 看板 #1OJy0pqh ] 作者: osanaosana (芋頭一顆多少錢) 看板: Database 標題: [SQL ] Update裡面進行子查詢失敗 時間: Tue Dec 13 17:32:33 2016 資料庫名稱:Mysql 資料庫版本:MySQL 5.5.44-MariaDB - MariaDB Server 內容/問題描述:( 使用別的辦法解決問題了 ) 問題的SQL: UPDATE `players` SET `players`.`xp` = `players`.`xp` + (SELECT count(oldplayers.`pid`) as addxp FROM (select tmp.`pid`,tmp.`loginTime` from `players` tmp) as oldplayers inner join `friends` tmpf on oldplayers.`pid` = tmpf.`playerB` where tmpf.`friendship` = 2 and oldplayers.`loginTime` > (NOW() - INTERVAL 3 DAY) and tmpf.`playerA` = 133223225) WHERE `pid` = 133223225 ; 中間那一段(SELECT ... tmpf.`playerA` = 133223225 ) 單獨執行可得到資料筆數2筆, 用上述語法卻是查到 0筆 大家好, 這個SQL問題的背景是我把玩家經驗值xp 設計成需要好友進行登入遊戲才會增加玩家經驗值xp, 相關條件有: 好友登入動作需要在過去三天內進行才有效 ( players.loginTime > (NOW() - INTERVAL 3 DAY) ) 好友關係成立 ( friends.friendship = 2 ) 以上條件成立時,根據查詢到的資料筆數, 用來增加玩家資料表players 的經驗值xp 玩家資料表 TABLE players pid xp (經驗值) loginTime (登入時間) 玩家的好友資料表 TABLE friends playerA (玩家本人pid ,資料內容可對應 players.pid) playerB (好友的pid, 資料內容可對應 players.pid) friendship (交往程度,2表示好友) SQL 1 (沒有問題, 可成功查詢SQL 2需要的 xp): SELECT count(oldplayers.`pid`) as addxp FROM (select tmp.`pid`,tmp.`loginTime` from `players` tmp) as oldplayers inner join `friends` tmpf on oldplayers.`pid` = tmpf.`playerB` WHERE tmpf.`friendship` = 2 and oldplayers.`loginTime` > (NOW() - INTERVAL 3 DAY) and tmpf.`playerA` = 133223225 SQL 2 (有問題的語法, 這是使用SQL 1的部分加入Update 裡, 查詢xp 完畢後同時更新xp, 語法執行沒有出現問題, 但是結果有問題, xp 沒有增加, 經測試後得知 SQL 1的部分得到0, Why ???) UPDATE `players` SET `players`.`xp` = `players`.`xp` + (SELECT count(oldplayers.`pid`) as addxp FROM (select tmp.`pid`,tmp.`loginTime` from `players` tmp) as oldplayers inner join `friends` tmpf on oldplayers.`pid` = tmpf.`playerB` where tmpf.`friendship` = 2 and oldplayers.`loginTime` > (NOW() - INTERVAL 3 DAY) and tmpf.`playerA` = 133223225) WHERE `pid` = 133223225 ; SQL 3 (上述的語法都只有針對單一玩家, 如果有N個玩家就要執行N次, SQL 3改為針對所有玩家進行一次處理) 本人能力不足做不出來... 先感謝大家看完了這複雜的問題... 懇請高手幫忙 ====================用別的辦法解決問題了 SQL n1: ( 查詢每個玩家須要加多少xp ) SELECT `playerA`,count(`playerA`) as addxp FROM `players` inner join `friends` on `players`.`pid` = `friends`.`playerB` WHERE `friendship` = 2 and `loginTime` > (NOW() - INTERVAL 3 DAY) GROUP BY `playerA` SQL n2: ( N個玩家更新xp 資料, 下列語法中的111 與 222 是SQL n1的查詢結果addxp 與 playerA, 再用中間程式( 我的是node.js )去拚出 N 句SQL n2給MySql執行) Update players Set xp = xp + 111 where pid = 222 ; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.250.106.218 ※ 文章網址: https://www.ptt.cc/bbs/Database/M.1481621555.A.D2B.html ※ 編輯: osanaosana (60.250.106.218), 12/13/2016 17:36:30 ※ 發信站: 批踢踢實業坊(ptt.cc) ※ 轉錄者: osanaosana (60.250.106.218), 12/13/2016 17:44:25 ※ 編輯: osanaosana (60.250.106.218), 12/13/2016 17:50:41 ※ 編輯: osanaosana (60.250.106.218), 12/14/2016 11:00:26
文章代碼(AID): #1OJyBxSq (GameDesign)