 |
ต้องการนับ COUNT เฉพาะบางข้อมูลที่อยู่ในฐานข้อมูลครับ |
|
 |
|
|
 |
 |
|
ลองแค่นี้ได้ไหมครับ
$strSQL = "select count(service) from detail";
|
 |
 |
 |
 |
Date :
2011-07-11 21:22:31 |
By :
grandraftz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลองเพิ่ม order by ซิคะ
|
 |
 |
 |
 |
Date :
2011-07-11 21:25:28 |
By :
naked13 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
SELECT service,COUNT(service) AS countservice FROM detail GROUP BY service ORDER BY countservice DESC
ข้อ2อ่านแล้วยังงงๆ แบบนี้รึป่าว นับข้อมูลที่ซ้ำกัน
SELECT service,COUNT(service) AS countservice FROM detail GROUP BY service HAVING countservice > 1 ORDER BY countservice DESC
|
ประวัติการแก้ไข 2011-07-11 22:04:11
 |
 |
 |
 |
Date :
2011-07-11 22:03:26 |
By :
avsqlz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ข้อ 2 นี้ผมจะให้นับข้อมูลในฟิวส์ service ทุกฟิวส์ที่มีข้อมูลซ้ำกับข้อมูลในฟิวส์ bg จะยกตัวอย่างให้เห็นภาพนะครับ
bg service
29 73
29 73
55 150
55 150
55 151
อยากให้ echo ออกมาเป็นตามผลลัพธ์อย่างนี้ครับ (ในที่นี้ผมจะ Search ข้อมูล bg(55) ออกมานะครับ)
150 2
151 1
|
 |
 |
 |
 |
Date :
2011-07-12 00:10:15 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เงียบเลย - -*
|
 |
 |
 |
 |
Date :
2011-07-12 08:50:31 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอาของคุณ avsqlz มาแปลงนิดหน่อยนะครับ ถ้าสร้าง DB ตาม No.4 บอกเขียนอย่างงี้นะครับ
Code (PHP)
select bb,service ,COUNT(service) from gg WHERE bb = 55 GROUP BY service ORDER BY service DESC
|
 |
 |
 |
 |
Date :
2011-07-12 09:03:38 |
By :
grandraftz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$strSQL="select bg,service,COUNT(service) AS countservice from detail WHERE bg = 55 GROUP BY service ORDER BY countservice DESC";
ERROR ครับ
Warning: mssql_query() [function.mssql-query]: message: Column 'detail.bg' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. (severity 16) in C:\AppServ\www\1799\report_business.php on line 226
Warning: mssql_query() [function.mssql-query]: Query failed in C:\AppServ\www\1799\report_business.php on line 226
Error Query [select bg,service,COUNT(service) AS countservice from detail WHERE bg = 55 GROUP BY service ORDER BY countservice DESC]
|
 |
 |
 |
 |
Date :
2011-07-12 09:14:23 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. คุณมี ฟิลด์ชื่อ bg หรือเปล่าครับ
2. ตรง ORDER BY เขียนแบบนี้ ORDER BY service DESC ไม่ต้องใส่ count
|
 |
 |
 |
 |
Date :
2011-07-12 09:21:17 |
By :
grandraftz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
1. มีฟิวส์ bg ครับ
2. error อยู่ครับ
Warning: mssql_query() [function.mssql-query]: message: Column 'detail.bg' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. (severity 16) in C:\AppServ\www\1799\report_business.php on line 228
Warning: mssql_query() [function.mssql-query]: Query failed in C:\AppServ\www\1799\report_business.php on line 228
Error Query [select bg,service,COUNT(service) from detail WHERE bg = '55' GROUP BY service ORDER BY service DESC]
|
 |
 |
 |
 |
Date :
2011-07-12 09:25:09 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณลองเอาไปใส่ใน phpmyadmi nของคุณดูก่อนนะครับ
Code (PHP)
SELECT bb, service, COUNT( service )
FROM detail
WHERE bb =55
GROUP BY service
ORDER BY service ASC
|
 |
 |
 |
 |
Date :
2011-07-12 09:27:03 |
By :
grandraftz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมใช้ sql server 2008 ไม่ได้ใช้ mysql นะครับ แล้วมันจะขึ้นเหรอคับ
|
 |
 |
 |
 |
Date :
2011-07-12 09:37:51 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมก็ไม่ทราบครับ เพราะไม่เคยได้ใช้ sql แต่ code ที่เขียนไปผมคิดว่ามันก็เป็นคำสั่งพื้นฐานนะครับ คิดว่ามันน่าจะรันได้
|
 |
 |
 |
 |
Date :
2011-07-12 09:47:43 |
By :
grandraftz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Warning: mssql_query() [function.mssql-query]: message: Column 'detail.bg' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. (severity 16) in C:\AppServ\www\1799\report_business.php on line 228
Warning: mssql_query() [function.mssql-query]: Query failed in C:\AppServ\www\1799\report_business.php on line 228
Error Query [select bg,service,COUNT(service) from detail WHERE bg = 55 GROUP BY service ORDER BY service DESC]
echo $strSQL; แล้วครับ Error เลย
|
 |
 |
 |
 |
Date :
2011-07-12 09:52:38 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลอง
SELECT bb, service, COUNT( service ) AS service
FROM detail
WHERE bb =55
GROUP BY service
ORDER BY service DESC
แต่ผมอยากให้เอา code ไป รันในตัว sql ตรงๆเลยมากกว่า ไม่ใช่รันผ่าน php และผมสงสัยว่าทำไมมันถึงบอกว่าบอก หา column bg ไม่เจอ
|
 |
 |
 |
 |
Date :
2011-07-12 10:22:54 |
By :
grandraftz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2011-07-12 10:29:32 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
dbo.detail ตรง dbo. คืออะไรเหรอครับ ใช่ชื่อมันด้วยหรือเปล่า พอดีผมไม่เคยใช้ SQL
|
 |
 |
 |
 |
Date :
2011-07-12 10:47:52 |
By :
grandraftz |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อ้อ มันเป็นชื่อขึ้นต้นอะครับ ตามด้วย dbo.[ชื่อตาราง]
|
 |
 |
 |
 |
Date :
2011-07-12 10:51:16 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
555+ ได้แล้วครับ ขอบคุณทุกคนนะครับ
|
 |
 |
 |
 |
Date :
2011-07-12 11:28:50 |
By :
MyZeus |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|