2014-11-06 41 views
0

我不知道如何解釋..我認爲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; 
+2

你是否必須在數據庫中完成這項工作?我猜想如果通過運行編程語言的結果集來實現這一點會更容易。 – 2014-11-06 18:41:58

+0

我100%同意@MarcoAurélioDeleu。 – 2014-11-06 19:54:38

+0

我在這種情況下使用了'GROUP_CONCAT':http://www.percona.com/blog/2013/10/22/the-power-of-mysqls-group_concat/ – Sonny 2014-11-06 21:34:14

回答

1

這是更好地在應用層做

你可以在mysql中做同樣的變量

select 
    @num := if(@user_name = user, @num + 1, 1) as row_number, 
    @user_name := user as dummy, 
    (case when @num = 1 then user else '' end) as user, col1, col2, col3, col4 

from Table1, (select @num := 0, @user_name:= '') t 

要獲得計數,使用GROUP BY

select col1, col2 , count(*) as TotalCount 
from table1 
group by col1,col2 
+0

更新: 這工作( (第一個'',中間',最後一個)作爲fullName,GROUP_CONCAT(casename order by casename SEPARATOR'
')FROM lnExpertWitness GROUP BY CONCAT(first,'',middle,'',last,lasta) SELECT CONCAT ) 但它顯然有一個大小限制。 :( 我的新問題(woudl我認爲這是比較容易)是我怎麼只能顯示DISTINCT(第一和最後一個名稱),而且還得到總的計數 即: 1尾1 第一1最後1 1尾1 第2尾2 第3最後3 首先3最後3 這將使我: First1最後1(3) 第2尾2(1) 第一3最後3(2) – whispers 2014-11-06 21:13:54

+0

@whispers,sele ct col1,col2,通過col1,col2從table1組中計數(*)。也接受答案。 – radar 2014-11-06 21:19:57

+0

答案接受..這一個^更容易理解! 更新inital post與問題的最後部分(這是最初的帖子的一部分..所以我理解的問題改變 – whispers 2014-11-06 21:24:52

相關問題