我不知道如何解釋..我認爲GROUP可能是我正在尋找..但我從來沒有使用它。獲取不同的用戶,但所有其他行相關聯
數據模式:
user1 col1 col2 col3 col4
user1 col1 col2 col3 col4
user1 col1 col2 col3 col4
user1 col1 col2 col3 col4
user2 col1 col2 col3 col4
user2 col1 col2 col3 col4
user2 col1 col2 col3 col4
user3 col1 col2 col3 col4
user3 col1 col2 col3 col4
user4 col1 col2 col3 col4
user4 col1 col2 col3 col4
user4 col1 col2 col3 col4
user4 col1 col2 col3 col4
etc..
我所希望做的是查詢數據庫並獲得不同用戶的名單,但有其他的cols它。
example:
user1 col1 col2 col3 col4
col1 col2 col3 col4
col1 col2 col3 col4
col1 col2 col3 col4
user2 col1 col2 col3 col4
col1 col2 col3 col4
col1 col2 col3 col4
user3 col1 col2 col3 col4
col1 col2 col3 col4
等。
我不需要多餘的用戶名..我只需要,有一次,但我需要與同一用戶..相關聯的所有其他的cols /行,直到一個新的不同的用戶是up/next
我期待使用GROUP?我不確定搜索/使用什麼來獲得這個結果。
我對MySQL的使用經驗不是很豐富,所以很容易閱讀/理解查詢或者tut/link。
顯然,Group_Concate有大小限制。(這是不適合我在這裏)
新的問題是,我該怎麼辦:
只能選擇/獲取DISTINCT名稱...但得到的計數有多少?
例子:
First1 Last1
First1 Last1
First2 Last2
First3 Last3
First3 Last3
First3 Last3
First3 Last3
這將返回:
First1 Last1 (2)
First2 Last2 (1)
First3 Last3 (4)
所以我只得到每一個獨特的名字一次..但也越來越共有多少次(算上?)其那裏?
更新:使用雷達的最後答覆(在他的評論)
我需要以某種方式這個融入我當前的搜索查詢:(我使用的PDO)
我需要以某種方式這個融入我當前查詢,因爲它只是使用類似%儘可能%字段的陣列上的用戶會使用搜索:
// define the list of fields (easier to loop through and check for values)
$searchFields = array('first', 'last', 'suffix', 'trialdate', 'wcity', 'wstate', 'plaintiff');
$searchConditions = array();
//loop through the defined fields & build query
foreach($searchFields as $searchField){
// if the field is set and not empty
if(isset($_POST[$searchField]) && $_POST[$searchField] != '') {
$searchConditions[] = "`$searchField` LIKE '%" . $_POST[$searchField] . "%'";
}
}
//build the query
$getExperts_sql = "SELECT * FROM $tablename";
// if there are conditions defined
if(count($searchConditions) > 0) {
// append the conditions
$getExperts_sql .= " WHERE " . implode (' AND ', $searchConditions); // you can change to 'OR', but I opt'd to apply the filters cumulative
}
我需要以某種方式得到什麼貼雷達..但也能搶行數據/內容填充頁面?
我想這會做到這一點:
select *, count(*) from lnExpertWitness group by first,last;
你是否必須在數據庫中完成這項工作?我猜想如果通過運行編程語言的結果集來實現這一點會更容易。 – 2014-11-06 18:41:58
我100%同意@MarcoAurélioDeleu。 – 2014-11-06 19:54:38
我在這種情況下使用了'GROUP_CONCAT':http://www.percona.com/blog/2013/10/22/the-power-of-mysqls-group_concat/ – Sonny 2014-11-06 21:34:14