我正在尝试使用临时表来存储查询所需的一些值。使用临时表的原因是我不想永久存储数据,因此不同的用户可以同时修改它。该数据只存储了一秒钟,所以我认为临时表是最好的方法。
问题在于,我试图使用它的方式似乎不对(如果我使用永久查询,则查询有效)。
这是一个查询示例:
CREATE TEMPORARY TABLE SearchMatches (PatternID int not null primary key, Matches int not null)
INSERT INTO SearchMatches (PatternID, Matches)
VALUES ('12605','1'),('12503','1'),('12587','2'),('12456','1'),
('12457','2'),('12486','2'),('12704','1'),(' 12686','1'),
('12531','2'),('12549','1'),('12604','1'),('12504','1'),
('12586','1'),('12548','1'),('12 530','1'),('12687','2'),
('12485','1'),('12705','1')
SELECT pat.id, signatures.signature, products.product, versions.version, builds.build, pat.log_file, sig_types.sig_type, pat.notes, pat.kb
FROM patterns AS pat
INNER JOIN signatures ON pat.signature = signatures.id
INNER JOIN products ON pat.product = products.id
INNER JOIN versions ON pat.version = versions.id
INNER JOIN builds ON pat.build = builds.id
INNER JOIN sig_types ON pat.sig_type = sig_types.id, SearchMatches AS sm
INNER JOIN patterns ON patterns.id = sm.PatternID
WHERE sm.Matches <> 0
ORDER BY sm.Matches DESC, products.product, versions.version, builds.build
LIMIT 0 , 50
有什么建议吗?
感谢。
答案 0 :(得分:0)
我在猜,但我认为查询应该是:
SELECT pat.id, signatures.signature, products.product, versions.version, builds.build, pat.log_file, sig_types.sig_type, pat.notes, pat.kb
FROM patterns AS pat
INNER JOIN signatures ON pat.signature = signatures.id
INNER JOIN products ON pat.product = products.id
INNER JOIN versions ON pat.version = versions.id
INNER JOIN builds ON pat.build = builds.id
INNER JOIN sig_types ON pat.sig_type = sig_types.id
INNER JOIN SearchMatches AS sm ON pat.id = sm.PatternID
WHERE sm.Matches <> 0
ORDER BY sm.Matches DESC, products.product, versions.version, builds.build
LIMIT 0 , 50;
这两行对我来说似乎不对:
INNER JOIN sig_types ON pat.sig_type = sig_types.id, SearchMatches AS sm INNER JOIN patterns ON patterns.id = sm.PatternID
如果你需要与patterns
表的两个连接,我认为你应该避免将逗号分隔与显式JOIN
子句混合。
答案 1 :(得分:0)
感谢您的回复。这是我最终得到的解决方案:
CREATE TEMPORARY TABLE SearchMatches ( PatternID int not null primary key, Matches int not null);
INSERT INTO SearchMatches
( PatternID, Matches ) VALUES ( 12605, 1 )
, ( 12503, 1 )
, ( 12587, 2 )
, ( 12456, 1 )
, ( 12457, 2 )
, ( 12486, 2 )
, ( 12704, 1 )
, ( 12686, 1 )
, ( 12531, 2 )
, ( 12549, 1 )
, ( 12604, 1 )
, ( 12504, 1 )
, ( 12586, 1 )
, ( 12548, 1 )
, ( 12530, 1 )
, ( 12687, 2 )
, ( 12485, 1 )
, ( 12705, 1 ) ;
SELECT pat.id
, signatures.signature
, products.product
, versions.version
, builds.build
, pat.log_file
, sig_types.sig_type
, pat.notes
, pat.kb FROM SearchMatches AS sm INNER JOIN patterns AS pat
ON pat.id = sm.PatternID INNER JOIN signatures
ON signatures.id = pat.signature INNER JOIN products
ON products.id = pat.product INNER JOIN versions
ON versions.id = pat.version INNER JOIN builds
ON builds.id = pat.build INNER JOIN sig_types
ON sig_types.id = pat.sig_type WHERE sm.Matches <> 0 ORDER BY sm.Matches DESC
, products.product
, versions.version
, builds.build LIMIT 0 , 50;