我是PHP的新手,我對數據庫不太瞭解。我有如下一個用戶表:多個查詢與使用PDO的PHP中的單個查詢?
------------------------------------------------------------------------
userid|firstname|lastname|password|Emailaddress|gender|agegroup|location
------------------------------------------------------------------------
| | | | | | |
| | | | | | |
我想對特定列值gender
,agegroup
和location
計數。如果我運行單個查詢,那麼數據不會按照我的需要返回。
$query="SELECT AgeGroupId as agegroupid,
count(AgeGroupId) as agegroupcount,
GENDER as gender,
count(GENDER) as gendercount,
Location as location,
count(Location) as locationcount
FROM userprofile
GROUP BY AgeGroupId, GENDER, Location";
如果我跑三個不同的查詢,然後我得到有些我想要的。
$query1="SELECT GENDER as gender,
count(GENDER) as gendercount
FROM userprofile
GROUP BY GENDER";
$query2="SELECT AgeGroupId as agegroupid,
count(AgeGroupId) as agegroupcount
FROM userprofile
GROUP BY AgeGroupId";
$query3="SELECT Location as location,
count(Location) as locationcount
FROM userprofile
GROUP BY Location";
所以,如果我跑三個查詢,然後我收到格式化,我想數據,如果我運行一個查詢比我需要的格式,使用PHP。那麼哪一個會更好?在PHP中運行單個查詢和處理數據並獲取格式化數據或運行三個查詢並獲取格式化數據?
思想:http://explainextended.com/2011/03/28/mysql-splitting-aggregate-queries/ – biziclop