我的网络应用程序需要从我的数据库中获取的信息,我只能为整个数据库生成,其中包含许多填充全新表的查询。任何人都可以向我建议一个查询返回与此填充表完全相同的结果吗?
首先,这是两个有信息的表。
CREATE OR REPLACE TABLE AS_Event(
AS_Id INT,
geneAbsentId VARCHAR(15),
genePresentId VARCHAR(15),
)ENGINE=INNODB;
CREATE OR REPLACE TABLE AS_ChoiceSupport(
AS_Id INT,
GeneId VARCHAR(15),
ESTId VARCHAR(20),
LibraryId INT,
)ENGINE=INNODB;
我已将其简化为我们需要的信息。 geneAbsentId和genePresentId(或AS_Choice)指向与AS_ChoiceSupport中的GeneId相同的表。每个事件中的每个AS_Choice都可以具有> = 0 AS_ChoiceSupport行。
现在,这是我正在创建的新表,但我需要它来代替单个查询。
CREATE TABLE AS_Support_Count(
AS_Id INT,
gaCount INT,
gpCount INT,
PRIMARY KEY(AS_Id),
FOREIGN KEY(AS_Id) REFERENCES AS_Event(AS_Id) ON UPDATE CASCADE ON DELETE CASCADE
)ENGINE=INNODB;
INSERT INTO AS_Support_Count(AS_Id) SELECT AS_Id FROM AS_Event
CREATE OR REPLACE VIEW TEMP AS SELECT e.AS_Id, COUNT(e.genePresentId) AS gpCount FROM AS_Event e, AS_ChoiceSupport c WHERE (e.AS_Id = c.AS_Id) AND e.genePresentId = c.GeneId GROUP BY e.AS_Id
UPDATE AS_Support_Count a, TEMP t
SET a.gpCount = t.gpCount
WHERE a.AS_Id = t.AS_Id
UPDATE AS_Support_Count
SET gpCount = 0
WHERE gpCount IS NULL
CREATE OR REPLACE VIEW TEMP AS SELECT e.AS_Id, COUNT(e.geneAbsentId) AS gaCount FROM AS_Event e, AS_ChoiceSupport c WHERE (e.AS_Id = c.AS_Id) AND e.geneAbsentId = c.GeneId GROUP BY e.AS_Id
UPDATE AS_Support_Count a, TEMP t
SET a.gaCount = t.gaCount
WHERE a.AS_Id = t.AS_Id
UPDATE AS_Support_Count
SET gaCount = 0
WHERE gaCount IS NULL
我需要将其缩写为单个查询的原因是我有时需要根据库ID获取事件的信息。即我需要知道AS_Event x有多少来自LibraryId的AS_ChoiceSupports。
我知道这是一个很大的问题所以我会将PokerStars的5美元转移给给我正确答案的人。
答案 0 :(得分:1)
我发现了如何从在线教程中转换数据 link text
这是我提出的查询
SELECT
COUNT(CASE WHEN e.genePresentId = c.GeneId THEN c.LibraryId ELSE NULL END) AS 'gpCount',
COUNT(CASE WHEN e.geneAbsentId = c.GeneId THEN c.LibraryId ELSE NULL END) AS 'gaCount'
FROM
AS_Event e, AS_ChoiceSupport c
WHERE e.AS_Id = c.AS_Id AND c.LibraryId = ? <sql:param value="${LibraryId}"/>
GROUP BY
c.AS_Id
ORDER BY
e.AS_Id
获得PokerStars扑克之星奖。但是,如果您有更好的答案,请为社区提供。
答案 1 :(得分:0)
这是否与AS_Support_Count中的select *产生相同的结果?
SELECT e.AS_Id
, COUNT(e.geneAbsentId) as gaGount
, COUNT(e.GenePresentId) as gpCount
FROM AS_Event e
JOIN AS_ChoiceSupport c
ON c.AS_Id = e.AS_Id
AND c.GeneId IN (e.geneAbesntId, e.genePresentId)
GROUP
BY e.AS_Id