select SUM 求和中帶模糊查詢怎麼弄?

來源:互聯網
上載者:User
$sql="select username,SUM(IF(types = 1, smoney, 0)) as t1 from record where 1=1".$s1." GROUP BY username";
現在得出的結果是:record 表中username = username的 types 欄位 = 1 的 smoney 欄位的總和 被賦值為變數 $t1
我想換一個查詢條件
希望得出結果是:record 表中username = username的 以及 regfrom 欄位中包含 nameuser 的 types 欄位 = 1 的 smoney 欄位的總和 被賦值為變數 $t1

請問sql語句該怎麼寫。資料庫:


回複討論(解決方案)

包含 nameuser 的 是什麼意思?

比如使用者admin 查詢就查詢到 nameuser 這個欄位中admin的資訊

$username=$_REQUEST['username'];

例:
查詢使用者為admin
username欄位中等於admin
以及 regfrom 欄位中包含 admin的
得出的結果為:10+10+20+30+20

查詢使用者為b123456
username欄位中等於b123456
以及 regfrom 欄位中包含 b123456 的
得出的結果為:20+30+20

$username=$_REQUEST['username'];$sql="select username, SUM(smoney) as t1 from record where username='$usename'";

regfrom 欄位中還要包含 $usename 這一段沒有呢?

把條件加上不就行了嗎?

regfrom 欄位中還要包含 $usename 這一段沒有呢?


舉一反三一下不就有了。

$sql="select username, SUM(smoney) as t1 from record where username='$usename' or instr(regfrom, '$username') >0";
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.