我正在建立一个网站,作为神经疾病的诊断工具。它是用html编写的,并通过PHP与MySQL数据库通信。向网站提供信息的主表结构如下:图像显示表结构,行表示神经病学条件,列提供与这些症状相关的信息

上表可以使用以下MySQL代码再现:
CREATE TABLE IF NOT EXISTS my_table (
`Condition` VARCHAR(22) CHARACTER SET utf8,
`Diarrhoea` INT,
`Headache` INT,
`Hyporeflexia` INT,
`Hypoaesthesia_Spinothalamic` INT
);
INSERT INTO my_table VALUES
('Abetalipoproteinaemia',1,NULL,1,NULL),
('Caffeine toxicity',1,1,NULL,NULL),
('Vitamin B12 deficiency',NULL,NULL,1,2);
SELECT * FROM my_table;单元格值如下:
我很难编写一个SQL查询,它将标识特定条件(m)的所有列(n),其中单元格(m,n) = 2的值。
到目前为止,我的阅读已经突出了关于枢轴表(我不知道如何使用它们来解决这个问题)和数据库规范化的想法,我认为这是不可能的,因为我正在同一张表上运行其他查询。
基于上表的一个例子:
任何帮助都将是非常感谢的-感谢您的时间!
发布于 2019-06-07 16:30:10
我认为整理这样的数据是比较常见的--为拼写错误或用词不当道歉,但如果你付了花生.
syptom condition exclusion
Abetalipoproteinaemia Diarrhoea 0
Abetalipoproteinaemia Hyporeflexia 0
Caffeine toxicity Diarrhoea 0
Caffeine toxicity Headache 0
Vitamin B12 deficiency Hyporeflexia 0
Vitamin B12 deficiency Hypoaesthesia Spinthalamic 1然后,您将采取这一或两个步骤,并有一个表的症状,一个表的条件,和一个表格,说明哪个症状与哪个条件,以及如何。
发布于 2019-06-07 16:58:26
如果表是按照规范的关系模式设计的,查询模式就会简单得多。
考虑此表单的查询返回的结果集:
SELECT v.condition
, v.symptom
, v.associated_or_excluded
FROM ( SELECT t1.`Condition` AS `condition`
, 'Diarrhoea' AS `symptom`
, t1.`Diarrhoea` AS `associated_or_excluded`
FROM mytable t1
UNION ALL
SELECT t2.`Condition`
, 'Headache'
, t2.`Headache`
FROM mytable t2
UNION ALL
SELECT t3.`Condition`
, 'Hyporeflexia'
, t3.`Hyporeflexia`
FROM mytable t3
UNION ALL
SELECT t4.`Condition`
, 'Hypoaesthesia_Spinothalamic'
, t4.`Hypoaesthesia_Spinothalamic`
FROM mytable t4
) v我们可以将该查询用作外部查询的内联视图(行源),也可以使用此查询INSERT ... SELECT的结果填充一个新表以进行转换。
使用该结果集,使用标准关系形式的数据,通过编写如下简单的查询,我们避免了这一困难:
SELECT t.symptom
FROM ( ... ) t
WHERE t.condition = 'Hyporeflexia'
AND t.associated_or_excluded = 2这将返回被排除在特定情况之外的症状。
(或者,用原来的问题来表示,其中值为2是m和n的交集)
请注意,( ... )被替换为表名或内联视图,返回上述查询的结果。
请注意,整个“挣扎”都在父类内部,内联视图查询以适当的形式表示数据。
SELECT t.symptom
FROM ( -- inline view query
SELECT t1.`Condition` AS `condition`
, 'Diarrhoea' AS `symptom`
, t1.`Diarrhoea` AS `associated_or_excluded`
FROM mytable t1
UNION ALL
SELECT t2.`Condition`
, 'Headache'
, t2.`Headache`
FROM mytable t2
UNION ALL
SELECT t3.`Condition`
, 'Hyporeflexia'
, t3.`Hyporeflexia`
FROM mytable t3
UNION ALL
SELECT t4.`Condition`
, 'Hypoaesthesia_Spinothalamic'
, t4.`Hypoaesthesia_Spinothalamic`
FROM mytable t4
) t
WHERE t.condition = 'Hyporeflexia'
AND t.associated_or_excluded = 2https://stackoverflow.com/questions/56497892
复制相似问题