使用变量列查看键值表

发布于 2024-12-07 09:04:34 字数 306 浏览 0 评论 0原文

我的数据位于键值表(在 MySql 中)中,其形状如下:

id, key, value

现在为了导出给我的用户,我需要将其转换为一个所有键作为列的表。 (实际上,大约 100 个键中只有大约 20 个需要位于该表中)

工作流程将是向我的用户提供,以便他们可以更正表并重新导入表。

我只是在编写一个相当复杂的选择命令来给我这样一个表。它已经是一个很长的命令了,我希望它不需要被调试。

我忍不住认为这应该是一个已经解决的问题;)

所以我希望任何人都可以为我提供一些线索。

I have my data in a key-value table (in MySql) which has the shape:

id, key, value

Now for export to my users I need to transform it into a table with all the keys as columns. (actually only ~20 of the ~100 keys need to be in that table)

The workflow would be to provide it my users so that they can correct the table and to reimport the tables.

I'm just in writing a quite complex select command to give me such a table. It already is some heck of long command and I hope it will not need to be debugged.

I can't help but think this should be an already solved problem ;)

So I'm hoping anyone can provide me with some clues.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年慕年 2024-12-14 09:04:34

到目前为止,我编写了一个可以构建视图的脚本。

DROP PROCEDURE IF EXISTS view_test;
DELIMITER //
CREATE PROCEDURE view_test()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE curr_prop VARCHAR(31);
DECLARE curr_table VARCHAR(31);
DECLARE fixed_prop VARCHAR(31);
DECLARE statement_a LONGTEXT DEFAULT 'SELECT pd.id,t0.date';
DECLARE statement_b LONGTEXT DEFAULT "FROM xxx.codes AS pd INNER JOIN    
     xxx.eav AS t0 ON (pd.id = t0.idX)";
DECLARE n_prop INT DEFAULT 0;

DECLARE cur1 CURSOR FOR SELECT DISTINCT prop FROM xxx.eav LIMIT 59;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

OPEN cur1;

main_loop: LOOP
FETCH cur1 INTO curr_prop;   
IF done
    THEN LEAVE main_loop;
END IF; 
SET fixed_prop = curr_prop;
SET fixed_prop = REPLACE(fixed_prop,'-','_');
SET fixed_prop = REPLACE(fixed_prop,'+','_');
SET fixed_prop = REPLACE(fixed_prop,' ','_');
SET n_prop = n_prop + 1;

SET curr_table = CONCAT('t',n_prop);
SET statement_a = CONCAT(statement_a,',\n',curr_table,'.value AS ', fixed_prop);
SET statement_b = CONCAT(statement_b,' \nLEFT JOIN xxx.eav AS ',curr_table,
    ' ON (',curr_table,'.idX=pd.id AND t0.date=',curr_table,'.date AND    
    ',curr_table,'.prop="',curr_prop,'")');    
END LOOP;
-- cleanup
CLOSE cur1;

SET @S = CONCAT('CREATE OR REPLACE VIEW auto_flat_table AS\n',statement_a,
 '\n',statement_b,
  '\nGROUP BY pd.id,t0.date'); -- ,'\nGROUP BY pd.id'

PREPARE stmt_auto_demo FROM @S;
EXECUTE stmt_auto_demo;
DEALLOCATE PREPARE stmt_auto_demo;
END //
DELIMITER ;

CALL view_test();

这将为我自己创建这样一个视图 - 尽管我有点困惑为什么这个 GROUP BY 是必要的。

虽然不是 SQL 开发人员,但我很确定我在这个程序中走了很长的路。

另外,我只能使用 61 个表进行连接,这真是太糟糕了。我想我们很快就会需要更多。

So far I hacked together a script that will construct a view.

DROP PROCEDURE IF EXISTS view_test;
DELIMITER //
CREATE PROCEDURE view_test()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE curr_prop VARCHAR(31);
DECLARE curr_table VARCHAR(31);
DECLARE fixed_prop VARCHAR(31);
DECLARE statement_a LONGTEXT DEFAULT 'SELECT pd.id,t0.date';
DECLARE statement_b LONGTEXT DEFAULT "FROM xxx.codes AS pd INNER JOIN    
     xxx.eav AS t0 ON (pd.id = t0.idX)";
DECLARE n_prop INT DEFAULT 0;

DECLARE cur1 CURSOR FOR SELECT DISTINCT prop FROM xxx.eav LIMIT 59;

DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;

OPEN cur1;

main_loop: LOOP
FETCH cur1 INTO curr_prop;   
IF done
    THEN LEAVE main_loop;
END IF; 
SET fixed_prop = curr_prop;
SET fixed_prop = REPLACE(fixed_prop,'-','_');
SET fixed_prop = REPLACE(fixed_prop,'+','_');
SET fixed_prop = REPLACE(fixed_prop,' ','_');
SET n_prop = n_prop + 1;

SET curr_table = CONCAT('t',n_prop);
SET statement_a = CONCAT(statement_a,',\n',curr_table,'.value AS ', fixed_prop);
SET statement_b = CONCAT(statement_b,' \nLEFT JOIN xxx.eav AS ',curr_table,
    ' ON (',curr_table,'.idX=pd.id AND t0.date=',curr_table,'.date AND    
    ',curr_table,'.prop="',curr_prop,'")');    
END LOOP;
-- cleanup
CLOSE cur1;

SET @S = CONCAT('CREATE OR REPLACE VIEW auto_flat_table AS\n',statement_a,
 '\n',statement_b,
  '\nGROUP BY pd.id,t0.date'); -- ,'\nGROUP BY pd.id'

PREPARE stmt_auto_demo FROM @S;
EXECUTE stmt_auto_demo;
DEALLOCATE PREPARE stmt_auto_demo;
END //
DELIMITER ;

CALL view_test();

That will create such a view for myself - though I'm a bit confused why this GROUP BY is neccesary.

Not being a SQL developer I'm pretty sure I took the long way in this program.

Also that I'm only able to use 61 tables for joins is a bummer. I think we will soon need more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文