简体   繁体   English

查询来自逗号分隔的JSON mysql字段的报告

[英]Query Reports from comma separated JSON mysql field

this is my first question. 这是我的第一个问题。 Apologies if I don't conform with the required rules. 如果我不符合规定的规则,请道歉。 I have tried my best and 我尽我所能

Problem-- I have a problem with a project am working on. 问题 - 我正在处理一个项目的问题。 It is medical based. 它以医疗为基础。 I have these three tables that I need to get reports from DB structure-- 我需要这三个表来从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

I also have patientsTable which has these key columns 我也有患有这些关键专栏的病人表

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

Lastly there is treatment table which contains 最后有治疗表,其中包含

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. 

I have already checked all results from https://stackoverflow.com/search?q=mysql+comma+separated+fields but none seems to solve my problem. 我已经检查了https://stackoverflow.com/search?q=mysql+comma+separated+fields的所有结果,但似乎没有解决我的问题。 I have checked on ways to restructure mysql schema but i would have too many tables to store the diseases diagnised from each patient. 我已经检查了重构mysql架构的方法,但是我会有太多的表来存储每个病人诊断出来的疾病。 In addition one patient might be diagnosed with multiple diseases and a disease can be added any time. 此外,一名患者可能被诊断患有多种疾病,并且可以随时添加疾病。 I might have almost 100 of them. 我可能有近100个。 If ID of Hypertension is 1 and arthritis is 2 a patient with both will have the diseaseTreated field with "1","2" in the treatment table 如果高血压的ID是1且关节炎是2,则两者都患有diseaseTreated field with "1","2" in the treatment table

Solution Expected-- I need to get reports for first time a specific disease has been treated for the first time or second time and more ie revisit for treatment of the same disease (if i wanted to know how many cases of hypertension are for the first time, and how many cases of hypertension are recurrent) 预期的解决方案 - 我需要首次获得第一次或第二次治疗特定疾病的报告,以及更多,即重新治疗相同疾病的治疗(如果我想知道有多少例高血压患者是第一次时间,以及有多少高血压病例复发)

Example - New cases report 示例 - 新案例报告

Disease | New Cases
Hypertension | 4

Example - Recurrent cases report 示例 - 复发病例报告

Disease | New Cases
Hypertension | 1
Arthritis    | 2

Am using Laravel I have attempted a lot of solutions but this is what i find too close to get what I need: Others I have attempted was looping through different array results i have queried from the database by using joins but I havent posted them because they have yielded nothing important. 我使用Laravel我尝试了很多解决方案,但这是我发现太接近无法得到我需要的东西:我尝试的其他人循环遍历不同的数组结果我通过使用连接从数据库查询但我没有发布它们因为他们没有什么重要的。

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

This one would get me the rows with a specific disease and by using php count() on the result I get an int as 3 which is OK but I haven't got to put it as I expect filter and differentiate new and recurrent cases. 这个会让我得到具有特定疾病的行,并通过在结果上使用php count()我得到一个int为3,这是好的,但我没有把它放在我期望过滤器和区分新的和经常性的情况。

Thanks, Any suggestion will be highly appreciated 谢谢,任何建议将受到高度赞赏

If the deseaseTreated column is json, couldn't you add an isNew key and when the patient comes in for a second visit, flip that key to false or something similar? 如果deseaseTreated列是json,你不能添加一个isNew键,当患者第二次访问时,将该键翻转为false或类似的东西?

Now when you want to make the report you can get counts on all of them. 现在,当您想要制作报告时,您可以获得所有这些报告。

//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