2016-06-27 36 views
0

MySQL表分開: 在我的設備表是這個如何行的值逐一與計數功能選項在MySQL

 facility_name         mbid   date   
    yoga,aerobics,table tennis,tai chi,   OM1111   2016-06-12 
    aerobics,tai chi,        OM1111   2016-06-12 

如何通過一個與MySQL的MBID分割行值之一:

 Facility_name    mbid  Number of count 
     yoga     OM1111   1 
     aerobics     OM1111   2 
     table tennis    OM1111   1 
     tai chi     OM1111   2 
+2

重組你的數據庫,以便它是正確的標準化,而這將不再是一個問題 –

+0

thaq編輯... –

回答

1
CREATE TABLE facility 
    (facility_name varchar(35), mbid varchar(6), date varchar(10)) 
; 

INSERT INTO facility 
    (facility_name, mbid, date) 
VALUES 
    ('yoga,aerobics,table tennis,tai chi,', 'OM1111', '2016-06-12'), 
    ('aerobics,tai chi,', 'OM1111', '2016-06-12') 
; 

腳本:

Select T.VALUE,T.mbid,COUNT(T.VALUE)Cnt FROM (
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(t.facility_name, ',', n.n), ',', -1) value,mbid 
    FROM facility t CROSS JOIN 
(
    SELECT a.N + b.N * 10 + 1 n 
    FROM 
    (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a 
    ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b 
    ORDER BY n 
) n 
WHERE n.n <= 1 + (LENGTH(t.facility_name) - LENGTH(REPLACE(t.facility_name, ',', ''))))T 
WHERE T.VALUE <> '' 
GROUP BY T.VALUE,T.mbid 
ORDER BY T.value 

如何通過在條件得到活動的數量日期功能:

Select facility.mbid,membership.name,membership.organization, 
membership.designation,membership.division, facility.VALUE `Facility Name`, 
COUNT(facility.VALUE)`Number of Activite` FROM (SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(facility.facility_name, ',', n.n), ',', -1) value,mbid FROM facility CROSS JOIN 
(
    SELECT a.N + b.N * 10 + 1 n 
    FROM 
    (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a 
    ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b 
    ORDER BY n 
) n 
WHERE n.n <= 1 + (LENGTH(facility.facility_name) -LENGTH(REPLACE(facility.facility_name, ',', ''))))T 
facility Inner Join membership ON facility.mbid=membership.mbid 
where facility.date Between '2016-06-04' and '2016-06-07' && 
    facility.VALUE <> '' 
    GROUP BY facility.VALUE,facility.mbid ORDER BY facility.value 
+0

你的sugesstion是不錯的..thanq .... –

+0

只是執行表並執行這個腳本,你會得到想要的結果。技術上我已經使用Cross Join和LEN(),SUBSTRING將逗號分隔開 – mohan111

+0

welcome @VallavanJ – mohan111

0

您的數據庫結構非常差。您應該每mbidfacility_name有一行。事實上,我不知道爲什麼mbiddate在兩行中是相同的。

如果我假設你已經在另一臺設備的名稱,那麼你可以使用:

select fn.facility_name, f.mbid, count(*) 
from facility f join 
    facility_names fn 
    on find_in_set(fn.facility_name, f.facility) > 0 
group by fn.facility_name, f.mbid; 

我要強調,雖然,雖然你可以做這樣的查詢,你真的需要修復您的數據結構。將值列表存儲在字符串中是將數據存儲在SQL數據庫中的錯誤方法。

+0

我的設施表有一個facility_name和mbid,我如何分割值的基礎,如下所示。 –