簡體   English   中英

查詢來自逗號分隔的JSON mysql字段的報告

[英]Query Reports from comma separated JSON mysql field

這是我的第一個問題。 如果我不符合規定的規則,請道歉。 我盡我所能

問題 - 我正在處理一個項目的問題。 它以醫療為基礎。 我需要這三個表來從DB結構中獲取報告 -

diseaseTable which has columns
diseaseID | diseaseName
diseaseID stores an integer i.e 1, 3, 4,..
dieseaseName stores name of disease e.g Hypertension, Malaria, ..e.tc

我也有患有這些關鍵專欄的病人表

patientID - which is an autoincrement int
patientName - name of patient
created_at - date and time of registration of the patient

最后有治療表,其中包含

treatmentID -autoincrement int
patient - contains the id of the patient which is a foreing key of patientsTable
created_at and updated_at for timestamps
diseaseTreated (which is my column of interest) Stores values in json comma separated fields. 

我已經檢查了https://stackoverflow.com/search?q=mysql+comma+separated+fields的所有結果,但似乎沒有解決我的問題。 我已經檢查了重構mysql架構的方法,但是我會有太多的表來存儲每個病人診斷出來的疾病。 此外,一名患者可能被診斷患有多種疾病,並且可以隨時添加疾病。 我可能有近100個。 如果高血壓的ID是1且關節炎是2,則兩者都患有diseaseTreated field with "1","2" in the treatment table

預期的解決方案 - 我需要首次獲得第一次或第二次治療特定疾病的報告,以及更多,即重新治療相同疾病的治療(如果我想知道有多少例高血壓患者是第一次時間,以及有多少高血壓病例復發)

示例 - 新案例報告

Disease | New Cases
Hypertension | 4

示例 - 復發病例報告

Disease | New Cases
Hypertension | 1
Arthritis    | 2

我使用Laravel我嘗試了很多解決方案,但這是我發現太接近無法得到我需要的東西:我嘗試的其他人循環遍歷不同的數組結果我通過使用連接從數據庫查詢但我沒有發布它們因為他們沒有什么重要的。

$diseaseID
$countedDiseaseCases = DB::select('select * from treatmentTable where JSON_CONTAINS(diseases, \'[\"'.$diseaseID.'\"]\')')

這個會讓我得到具有特定疾病的行,並通過在結果上使用php count()我得到一個int為3,這是好的,但我沒有把它放在我期望過濾器和區分新的和經常性的情況。

謝謝,任何建議將受到高度贊賞

如果deseaseTreated列是json,你不能添加一個isNew鍵,當患者第二次訪問時,將該鍵翻轉為false或類似的東西?

現在,當您想要制作報告時,您可以獲得所有這些報告。

//example deseaseTreated value 
{
  "deseases": [
    {
      "deseaseID":"Value", 
      "isNew":Boolean,
      ...
    }, 
    {...}
  ]
}
$records = DB::select('select * from treatment');
$diseases = DB::select('select * from diseases');
foreach ($diseases as $disease) {
    $disease['count_new'] = 0;
}
foreach ($records as $record) {
    $json = json_decode($record['diseaseTreated']);
    foreach ($json['diseases'] as disease_index) {
    if ($disease_index['isNew']) {
        foreach ($diseases as &$d) {
            if ($d['diseaseID'] == $disease_index['diseaseID']) {
                $d['count_new']++;
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM